source: trunk/src/devices/pnp_device.c

Last change on this file was 6323, checked in by mjones, 5 years ago

Fix an infinite loop in pnp_get_ioresource(), which freezes coreboot if
a rare condition arises.

Based on findings by Alexandru Gagniuc <mr.nuke.me@…>

Signed-off-by: Stefan Reinauer <reinauer@…>

Acked-by: Alexandru Gagniuc <mr.nuke.me@…>

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2004 Linux Networx
5 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6 * Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
7 * Copyright (C) 2005 Tyan
8 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22 */
23
24#include <console/console.h>
25#include <stdlib.h>
26#include <stdint.h>
27#include <bitops.h>
28#include <string.h>
29#include <arch/io.h>
30#include <device/device.h>
31#include <device/pnp.h>
32
33/* PNP fundamental operations */
34
35void pnp_write_config(device_t dev, u8 reg, u8 value)
36{
37        outb(reg, dev->path.pnp.port);
38        outb(value, dev->path.pnp.port + 1);
39}
40
41u8 pnp_read_config(device_t dev, u8 reg)
42{
43        outb(reg, dev->path.pnp.port);
44        return inb(dev->path.pnp.port + 1);
45}
46
47void pnp_set_logical_device(device_t dev)
48{
49        pnp_write_config(dev, 0x07, dev->path.pnp.device & 0xff);
50}
51
52void pnp_set_enable(device_t dev, int enable)
53{
54        u8 tmp, bitpos;
55
56        tmp = pnp_read_config(dev, 0x30);
57
58        /* Handle virtual devices, which share the same LDN register. */
59        bitpos = (dev->path.pnp.device >> 8) & 0x7;
60
61        if (enable)
62                tmp |= (1 << bitpos);
63        else
64                tmp &= ~(1 << bitpos);
65
66        pnp_write_config(dev, 0x30, tmp);
67}
68
69int pnp_read_enable(device_t dev)
70{
71        u8 tmp, bitpos;
72
73        tmp = pnp_read_config(dev, 0x30);
74
75        /* Handle virtual devices, which share the same LDN register. */
76        bitpos = (dev->path.pnp.device >> 8) & 0x7;
77
78        return !!(tmp & (1 << bitpos));
79}
80
81void pnp_set_iobase(device_t dev, u8 index, u16 iobase)
82{
83        /* Index == 0x60 or 0x62. */
84        pnp_write_config(dev, index + 0, (iobase >> 8) & 0xff);
85        pnp_write_config(dev, index + 1, iobase & 0xff);
86}
87
88void pnp_set_irq(device_t dev, u8 index, u8 irq)
89{
90        /* Index == 0x70 or 0x72. */
91        pnp_write_config(dev, index, irq);
92}
93
94void pnp_set_drq(device_t dev, u8 index, u8 drq)
95{
96        /* Index == 0x74. */
97        pnp_write_config(dev, index, drq & 0xff);
98}
99
100/* PNP device operations */
101
102void pnp_read_resources(device_t dev)
103{
104        return;
105}
106
107static void pnp_set_resource(device_t dev, struct resource *resource)
108{
109        if (!(resource->flags & IORESOURCE_ASSIGNED)) {
110                printk(BIOS_ERR, "ERROR: %s %02lx %s size: 0x%010Lx "
111                       "not assigned\n", dev_path(dev), resource->index,
112                       resource_type(resource), resource->size);
113                return;
114        }
115
116        /* Now store the resource. */
117        if (resource->flags & IORESOURCE_IO) {
118                pnp_set_iobase(dev, resource->index, resource->base);
119        } else if (resource->flags & IORESOURCE_DRQ) {
120                pnp_set_drq(dev, resource->index, resource->base);
121        } else if (resource->flags & IORESOURCE_IRQ) {
122                pnp_set_irq(dev, resource->index, resource->base);
123        } else {
124                printk(BIOS_ERR, "ERROR: %s %02lx unknown resource type\n",
125                       dev_path(dev), resource->index);
126                return;
127        }
128        resource->flags |= IORESOURCE_STORED;
129
130        report_resource_stored(dev, resource, "");
131}
132
133void pnp_set_resources(device_t dev)
134{
135        struct resource *res;
136
137        /* Select the logical device (LDN). */
138        pnp_set_logical_device(dev);
139
140        /* Paranoia says I should disable the device here... */
141        for (res = dev->resource_list; res; res = res->next)
142                pnp_set_resource(dev, res);
143}
144
145void pnp_enable_resources(device_t dev)
146{
147        pnp_set_logical_device(dev);
148        pnp_set_enable(dev, 1);
149}
150
151void pnp_enable(device_t dev)
152{
153        if (!dev->enabled) {
154                pnp_set_logical_device(dev);
155                pnp_set_enable(dev, 0);
156        }
157}
158
159struct device_operations pnp_ops = {
160        .read_resources   = pnp_read_resources,
161        .set_resources    = pnp_set_resources,
162        .enable_resources = pnp_enable_resources,
163        .enable           = pnp_enable,
164};
165
166/* PNP chip operations */
167
168static void pnp_get_ioresource(device_t dev, u8 index, struct io_info *info)
169{
170        struct resource *resource;
171        unsigned moving, gran, step;
172
173        if (!info->mask) {
174                printk(BIOS_ERR, "ERROR: device %s index %d has no mask.\n",
175                                dev_path(dev), index);
176                return;
177        }
178
179        resource = new_resource(dev, index);
180
181        /* Initilize the resource. */
182        resource->limit = 0xffff;
183        resource->flags |= IORESOURCE_IO;
184
185        /* Get the resource size... */
186
187        moving = info->mask;
188        gran = 15;
189        step = 1 << gran;
190
191        /* Find the first bit that moves. */
192        while ((moving & step) == 0) {
193                gran--;
194                step >>= 1;
195        }
196
197        /* Now find the first bit that does not move. */
198        while ((moving & step) != 0) {
199                gran--;
200                step >>= 1;
201        }
202
203        /*
204         * Of the moving bits the last bit in the first group,
205         * tells us the size of this resource.
206         */
207        if ((moving & step) == 0) {
208                gran++;
209                step <<= 1;
210        }
211
212        /* Set the resource size and alignment. */
213        resource->gran  = gran;
214        resource->align = gran;
215        resource->limit = info->mask | (step - 1);
216        resource->size  = 1 << gran;
217}
218
219static void get_resources(device_t dev, struct pnp_info *info)
220{
221        struct resource *resource;
222
223        if (info->flags & PNP_IO0)
224                pnp_get_ioresource(dev, PNP_IDX_IO0, &info->io0);
225        if (info->flags & PNP_IO1)
226                pnp_get_ioresource(dev, PNP_IDX_IO1, &info->io1);
227        if (info->flags & PNP_IO2)
228                pnp_get_ioresource(dev, PNP_IDX_IO2, &info->io2);
229        if (info->flags & PNP_IO3)
230                pnp_get_ioresource(dev, PNP_IDX_IO3, &info->io3);
231
232        if (info->flags & PNP_IRQ0) {
233                resource = new_resource(dev, PNP_IDX_IRQ0);
234                resource->size = 1;
235                resource->flags |= IORESOURCE_IRQ;
236        }
237        if (info->flags & PNP_IRQ1) {
238                resource = new_resource(dev, PNP_IDX_IRQ1);
239                resource->size = 1;
240                resource->flags |= IORESOURCE_IRQ;
241        }
242
243        if (info->flags & PNP_DRQ0) {
244                resource = new_resource(dev, PNP_IDX_DRQ0);
245                resource->size = 1;
246                resource->flags |= IORESOURCE_DRQ;
247        }
248        if (info->flags & PNP_DRQ1) {
249                resource = new_resource(dev, PNP_IDX_DRQ1);
250                resource->size = 1;
251                resource->flags |= IORESOURCE_DRQ;
252        }
253
254        /*
255         * These are not IRQs, but set the flag to have the
256         * resource allocator do the right thing.
257         */
258        if (info->flags & PNP_EN) {
259                resource = new_resource(dev, PNP_IDX_EN);
260                resource->size = 1;
261                resource->flags |= IORESOURCE_IRQ;
262        }
263        if (info->flags & PNP_MSC0) {
264                resource = new_resource(dev, PNP_IDX_MSC0);
265                resource->size = 1;
266                resource->flags |= IORESOURCE_IRQ;
267        }
268        if (info->flags & PNP_MSC1) {
269                resource = new_resource(dev, PNP_IDX_MSC1);
270                resource->size = 1;
271                resource->flags |= IORESOURCE_IRQ;
272        }
273}
274
275void pnp_enable_devices(device_t base_dev, struct device_operations *ops,
276                        unsigned int functions, struct pnp_info *info)
277{
278        struct device_path path;
279        device_t dev;
280        int i;
281
282        path.type = DEVICE_PATH_PNP;
283        path.pnp.port = base_dev->path.pnp.port;
284
285        /* Setup the ops and resources on the newly allocated devices. */
286        for (i = 0; i < functions; i++) {
287                /* Skip logical devices this Super I/O doesn't have. */
288                if (info[i].function == -1)
289                        continue;
290
291                path.pnp.device = info[i].function;
292                dev = alloc_find_dev(base_dev->bus, &path);
293
294                /* Don't initialize a device multiple times. */
295                if (dev->ops)
296                        continue;
297
298                if (info[i].ops == 0)
299                        dev->ops = ops;
300                else
301                        dev->ops = info[i].ops;
302
303                get_resources(dev, &info[i]);
304        }
305}
Note: See TracBrowser for help on using the repository browser.