210ef7f6576c22e183397dc213e7010b663ec674
[linux-flexiantxendom0-3.2.10.git] / drivers / acpi / pci_root.c
1 /*
2  *  pci_root.c - ACPI PCI Root Bridge Driver ($Revision: 40 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/proc_fs.h>
31 #include <linux/spinlock.h>
32 #include <linux/pm.h>
33 #include <linux/pci.h>
34 #include <linux/acpi.h>
35 #include <acpi/acpi_bus.h>
36 #include <acpi/acpi_drivers.h>
37
38
39 #define _COMPONENT              ACPI_PCI_COMPONENT
40 ACPI_MODULE_NAME                ("pci_root")
41
42 #define ACPI_PCI_ROOT_CLASS             "pci_bridge"
43 #define ACPI_PCI_ROOT_HID               "PNP0A03"
44 #define ACPI_PCI_ROOT_DRIVER_NAME       "ACPI PCI Root Bridge Driver"
45 #define ACPI_PCI_ROOT_DEVICE_NAME       "PCI Root Bridge"
46
47 extern struct pci_ops *pci_root_ops;
48
49 static int acpi_pci_root_add (struct acpi_device *device);
50 static int acpi_pci_root_remove (struct acpi_device *device, int type);
51
52 static struct acpi_driver acpi_pci_root_driver = {
53         .name =         ACPI_PCI_ROOT_DRIVER_NAME,
54         .class =        ACPI_PCI_ROOT_CLASS,
55         .ids =          ACPI_PCI_ROOT_HID,
56         .ops =          {
57                                 .add =    acpi_pci_root_add,
58                                 .remove = acpi_pci_root_remove,
59                         },
60 };
61
62  struct acpi_pci_root {
63         struct list_head        node;
64         acpi_handle             handle;
65         struct acpi_pci_id      id;
66         struct pci_bus          *bus;
67         u64                     mem_tra;
68         u64                     io_tra;
69  };
70
71 struct list_head                acpi_pci_roots;
72
73
74 void
75 acpi_pci_get_translations (
76         struct acpi_pci_id      *id,
77         u64                     *mem_tra,
78         u64                     *io_tra)
79 {
80         struct list_head        *node = NULL;
81         struct acpi_pci_root    *entry;
82
83         /* TBD: Locking */
84         list_for_each(node, &acpi_pci_roots) {
85                 entry = list_entry(node, struct acpi_pci_root, node);
86                 if ((id->segment == entry->id.segment)
87                         && (id->bus == entry->id.bus)) {
88                         *mem_tra = entry->mem_tra;
89                         *io_tra = entry->io_tra;
90                         return;
91                 }
92         }
93
94         *mem_tra = 0;
95         *io_tra = 0;
96 }
97
98
99 static u64
100 acpi_pci_root_bus_tra (
101        struct acpi_resource     *resource,
102        int                      type)
103 {
104         struct acpi_resource_address16 *address16;
105         struct acpi_resource_address32 *address32;
106         struct acpi_resource_address64 *address64;
107
108         while (1) {
109                 switch (resource->id) {
110                 case ACPI_RSTYPE_END_TAG:
111                         return 0;
112
113                 case ACPI_RSTYPE_ADDRESS16:
114                         address16 = (struct acpi_resource_address16 *) &resource->data;
115                         if (type == address16->resource_type) {
116                                 return address16->address_translation_offset;
117                         }
118                         break;
119
120                 case ACPI_RSTYPE_ADDRESS32:
121                         address32 = (struct acpi_resource_address32 *) &resource->data;
122                         if (type == address32->resource_type) {
123                                 return address32->address_translation_offset;
124                         }
125                         break;
126
127                 case ACPI_RSTYPE_ADDRESS64:
128                         address64 = (struct acpi_resource_address64 *) &resource->data;
129                         if (type == address64->resource_type) {
130                                 return address64->address_translation_offset;
131                         }
132                         break;
133                 }
134                 resource = ACPI_PTR_ADD (struct acpi_resource,
135                                 resource, resource->length);
136         }
137
138         return 0;
139 }
140
141
142 static int
143 acpi_pci_evaluate_crs (
144         struct acpi_pci_root    *root)
145 {
146         acpi_status             status;
147         struct acpi_buffer      buffer = {ACPI_ALLOCATE_BUFFER, NULL};
148
149         ACPI_FUNCTION_TRACE("acpi_pci_evaluate_crs");
150
151         status = acpi_get_current_resources (root->handle, &buffer);
152         if (ACPI_FAILURE(status))
153                 return_VALUE(-ENODEV);
154
155         root->io_tra = acpi_pci_root_bus_tra ((struct acpi_resource *)
156                         buffer.pointer, ACPI_IO_RANGE);
157         root->mem_tra = acpi_pci_root_bus_tra ((struct acpi_resource *)
158                         buffer.pointer, ACPI_MEMORY_RANGE);
159
160         acpi_os_free(buffer.pointer);
161         return_VALUE(0);
162 }
163
164
165 static int
166 acpi_pci_root_add (
167         struct acpi_device      *device)
168 {
169         int                     result = 0;
170         struct acpi_pci_root    *root = NULL;
171         acpi_status             status = AE_OK;
172         unsigned long           value = 0;
173         acpi_handle             handle = NULL;
174
175         ACPI_FUNCTION_TRACE("acpi_pci_root_add");
176
177         if (!device)
178                 return_VALUE(-EINVAL);
179
180         root = kmalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
181         if (!root)
182                 return_VALUE(-ENOMEM);
183         memset(root, 0, sizeof(struct acpi_pci_root));
184
185         root->handle = device->handle;
186         sprintf(acpi_device_name(device), "%s", ACPI_PCI_ROOT_DEVICE_NAME);
187         sprintf(acpi_device_class(device), "%s", ACPI_PCI_ROOT_CLASS);
188         acpi_driver_data(device) = root;
189
190         /*
191          * TBD: Doesn't the bus driver automatically set this?
192          */
193         device->ops.bind = acpi_pci_bind;
194
195         /* 
196          * Segment
197          * -------
198          * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
199          */
200         status = acpi_evaluate_integer(root->handle, METHOD_NAME__SEG, NULL, 
201                 &value);
202         switch (status) {
203         case AE_OK:
204                 root->id.segment = (u16) value;
205                 printk("_SEG exists! Unsupported. Abort.\n");
206                 BUG();
207                 break;
208         case AE_NOT_FOUND:
209                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
210                         "Assuming segment 0 (no _SEG)\n"));
211                 root->id.segment = 0;
212                 break;
213         default:
214                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _SEG\n"));
215                 result = -ENODEV;
216                 goto end;
217         }
218
219         /* 
220          * Bus
221          * ---
222          * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
223          */
224         status = acpi_evaluate_integer(root->handle, METHOD_NAME__BBN, NULL, 
225                 &value);
226         switch (status) {
227         case AE_OK:
228                 root->id.bus = (u16) value;
229                 break;
230         case AE_NOT_FOUND:
231                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
232                 root->id.bus = 0;
233                 break;
234         default:
235                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _BBN\n"));
236                 result = -ENODEV;
237                 goto end;
238         }
239
240         /*
241          * Device & Function
242          * -----------------
243          * Obtained from _ADR (which has already been evaluated for us).
244          */
245         root->id.device = device->pnp.bus_address >> 16;
246         root->id.function = device->pnp.bus_address & 0xFFFF;
247
248         /*
249          * Evaluate _CRS to get root bridge resources
250          * TBD: Need PCI interface for enumeration/configuration of roots.
251          */
252         acpi_pci_evaluate_crs(root);
253
254         /* TBD: Locking */
255         list_add_tail(&root->node, &acpi_pci_roots);
256
257         printk(KERN_INFO PREFIX "%s [%s] (%02x:%02x)\n", 
258                 acpi_device_name(device), acpi_device_bid(device),
259                 root->id.segment, root->id.bus);
260
261         /*
262          * Scan the Root Bridge
263          * --------------------
264          * Must do this prior to any attempt to bind the root device, as the
265          * PCI namespace does not get created until this call is made (and 
266          * thus the root bridge's pci_dev does not exist).
267          */
268         root->bus = pcibios_scan_root(root->id.bus);
269         if (!root->bus) {
270                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
271                         "Bus %02x:%02x not present in PCI namespace\n", 
272                         root->id.segment, root->id.bus));
273                 result = -ENODEV;
274                 goto end;
275         }
276
277         /*
278          * Attach ACPI-PCI Context
279          * -----------------------
280          * Thus binding the ACPI and PCI devices.
281          */
282         result = acpi_pci_bind_root(device, &root->id, root->bus);
283         if (result)
284                 goto end;
285
286         /*
287          * PCI Routing Table
288          * -----------------
289          * Evaluate and parse _PRT, if exists.
290          */
291         status = acpi_get_handle(root->handle, METHOD_NAME__PRT, &handle);
292         if (ACPI_SUCCESS(status))
293                 result = acpi_pci_irq_add_prt(root->handle, root->id.segment,
294                         root->id.bus);
295
296 end:
297         if (result)
298                 kfree(root);
299
300         return_VALUE(result);
301 }
302
303
304 static int
305 acpi_pci_root_remove (
306         struct acpi_device      *device,
307         int                     type)
308 {
309         struct acpi_pci_root    *root = NULL;
310
311         ACPI_FUNCTION_TRACE("acpi_pci_root_remove");
312
313         if (!device || !acpi_driver_data(device))
314                 return_VALUE(-EINVAL);
315
316         root = (struct acpi_pci_root *) acpi_driver_data(device);
317
318         kfree(root);
319
320         return_VALUE(0);
321 }
322
323
324 static int __init acpi_pci_root_init (void)
325 {
326         ACPI_FUNCTION_TRACE("acpi_pci_root_init");
327
328         if (acpi_disabled)
329                 return_VALUE(0);
330
331         /* DEBUG:
332         acpi_dbg_layer = ACPI_PCI_COMPONENT;
333         acpi_dbg_level = 0xFFFFFFFF;
334          */
335
336         INIT_LIST_HEAD(&acpi_pci_roots);
337
338         if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
339                 return_VALUE(-ENODEV);
340
341         return_VALUE(0);
342 }
343
344 subsys_initcall(acpi_pci_root_init);
345