2 * ACPI PCI HotPlug PCI configuration space management
4 * Copyright (C) 1995,2001 Compaq Computer Corporation
5 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2001,2002 IBM Corp.
7 * Copyright (C) 2002 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
8 * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
9 * Copyright (C) 2002 NEC Corporation
11 * All rights reserved.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or (at
16 * your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
21 * NON INFRINGEMENT. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 * Send feedback to <t-kochi@bq.jp.nec.com>
32 #include <linux/init.h>
33 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/pci.h>
37 #include "pci_hotplug.h"
40 #define MY_NAME "acpiphp_pci"
42 static void acpiphp_configure_irq (struct pci_dev *dev);
45 /* allocate mem/pmem/io resource to a new function */
46 static int init_config_space (struct acpiphp_func *func)
59 struct acpiphp_bridge *bridge;
60 struct pci_resource *res;
64 bridge = func->slot->bridge;
65 bus = bridge->pci_bus;
66 devfn = PCI_DEVFN(func->slot->device, func->function);
68 for (count = 0; address[count]; count++) { /* for 6 BARs */
69 pci_bus_write_config_dword(bus, devfn, address[count], 0xFFFFFFFF);
70 pci_bus_read_config_dword(bus, devfn, address[count], &bar);
72 if (!bar) /* This BAR is not implemented */
75 dbg("Device %02x.%d BAR %d wants %x\n", PCI_SLOT(devfn),
76 PCI_FUNC(devfn), count, bar);
78 if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
81 len = bar & (PCI_BASE_ADDRESS_IO_MASK & 0xFFFF);
82 len = len & ~(len - 1);
84 dbg("len in IO %x, BAR %d\n", len, count);
86 spin_lock(&bridge->res_lock);
87 res = acpiphp_get_io_resource(&bridge->io_head, len);
88 spin_unlock(&bridge->res_lock);
91 err("cannot allocate requested io for %02x:%02x.%d len %x\n",
92 bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn), len);
95 pci_bus_write_config_dword(bus, devfn, address[count], (u32)res->base);
96 res->next = func->io_head;
101 if (bar & PCI_BASE_ADDRESS_MEM_PREFETCH) {
104 len = bar & 0xFFFFFFF0;
107 dbg("len in PFMEM %x, BAR %d\n", len, count);
109 spin_lock(&bridge->res_lock);
110 res = acpiphp_get_resource(&bridge->p_mem_head, len);
111 spin_unlock(&bridge->res_lock);
114 err("cannot allocate requested pfmem for %02x:%02x.%d len %x\n",
115 bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn), len);
119 pci_bus_write_config_dword(bus, devfn, address[count], (u32)res->base);
121 if (bar & PCI_BASE_ADDRESS_MEM_TYPE_64) { /* takes up another dword */
122 dbg("inside the pfmem 64 case, count %d\n", count);
124 pci_bus_write_config_dword(bus, devfn, address[count], (u32)(res->base >> 32));
127 res->next = func->p_mem_head;
128 func->p_mem_head = res;
133 len = bar & 0xFFFFFFF0;
136 dbg("len in MEM %x, BAR %d\n", len, count);
138 spin_lock(&bridge->res_lock);
139 res = acpiphp_get_resource(&bridge->mem_head, len);
140 spin_unlock(&bridge->res_lock);
143 err("cannot allocate requested pfmem for %02x:%02x.%d len %x\n",
144 bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn), len);
148 pci_bus_write_config_dword(bus, devfn, address[count], (u32)res->base);
150 if (bar & PCI_BASE_ADDRESS_MEM_TYPE_64) {
151 /* takes up another dword */
152 dbg("inside mem 64 case, reg. mem, count %d\n", count);
154 pci_bus_write_config_dword(bus, devfn, address[count], (u32)(res->base >> 32));
157 res->next = func->mem_head;
158 func->mem_head = res;
164 /* disable expansion rom */
165 pci_bus_write_config_dword(bus, devfn, PCI_ROM_ADDRESS, 0x00000000);
172 static int configure_pci_dev (struct pci_dev_wrapped *wrapped_dev, struct pci_bus_wrapped *wrapped_bus)
175 struct acpiphp_func *func;
176 struct acpiphp_bridge *bridge;
179 func = (struct acpiphp_func *)wrapped_dev->data;
180 bridge = (struct acpiphp_bridge *)wrapped_bus->data;
181 dev = wrapped_dev->dev;
183 /* TBD: support PCI-to-PCI bridge case */
184 if (!func || !bridge)
187 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, bridge->hpp.cache_line_size);
188 pci_write_config_byte(dev, PCI_LATENCY_TIMER, bridge->hpp.latency_timer);
190 pci_read_config_word(dev, PCI_COMMAND, &tmp);
191 if (bridge->hpp.enable_SERR)
192 tmp |= PCI_COMMAND_SERR;
193 if (bridge->hpp.enable_PERR)
194 tmp |= PCI_COMMAND_PARITY;
195 //tmp |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
196 pci_write_config_word(dev, PCI_COMMAND, tmp);
198 acpiphp_configure_irq(dev);
199 #ifdef CONFIG_PROC_FS
200 pci_proc_attach_device(dev);
202 pci_announce_device_to_drivers(dev);
203 info("Device %s configured\n", dev->slot_name);
209 static int is_pci_dev_in_use (struct pci_dev* dev)
212 * dev->driver will be set if the device is in use by a new-style
213 * driver -- otherwise, check the device's regions to see if any
214 * driver has claimed them
219 if (dev->driver) return 1; //assume driver feels responsible
221 for (i = 0; !dev->driver && !inuse && (i < 6); i++) {
222 if (!pci_resource_start(dev, i))
225 if (pci_resource_flags(dev, i) & IORESOURCE_IO)
226 inuse = check_region(pci_resource_start(dev, i),
227 pci_resource_len(dev, i));
228 else if (pci_resource_flags(dev, i) & IORESOURCE_MEM)
229 inuse = check_mem_region(pci_resource_start(dev, i),
230 pci_resource_len(dev, i));
237 static int pci_hp_remove_device (struct pci_dev *dev)
239 if (is_pci_dev_in_use(dev)) {
240 err("***Cannot safely power down device -- "
241 "it appears to be in use***\n");
244 pci_remove_device(dev);
249 /* remove device driver */
250 static int unconfigure_pci_dev_driver (struct pci_dev_wrapped *wrapped_dev, struct pci_bus_wrapped *wrapped_bus)
252 struct pci_dev *dev = wrapped_dev->dev;
254 dbg("attempting removal of driver for device %s\n", dev->slot_name);
256 /* Now, remove the Linux Driver Representation */
258 if (dev->driver->remove) {
259 dev->driver->remove(dev);
260 dbg("driver was properly removed\n");
265 return is_pci_dev_in_use(dev);
269 /* remove pci_dev itself from system */
270 static int unconfigure_pci_dev (struct pci_dev_wrapped *wrapped_dev, struct pci_bus_wrapped *wrapped_bus)
272 struct pci_dev *dev = wrapped_dev->dev;
274 /* Now, remove the Linux Representation */
276 if (pci_hp_remove_device(dev) == 0) {
277 info("Device %s removed\n", dev->slot_name);
278 kfree(dev); /* Now, remove */
280 return -1; /* problems while freeing, abort visitation */
288 /* remove pci_bus itself from system */
289 static int unconfigure_pci_bus (struct pci_bus_wrapped *wrapped_bus, struct pci_dev_wrapped *wrapped_dev)
291 struct pci_bus *bus = wrapped_bus->bus;
293 #ifdef CONFIG_PROC_FS
294 /* Now, remove the Linux Representation */
296 pci_proc_detach_bus(bus);
299 /* the cleanup code should live in the kernel ... */
300 bus->self->subordinate = NULL;
301 /* unlink from parent bus */
302 list_del(&bus->node);
312 /* detect_used_resource - subtract resource under dev from bridge */
313 static int detect_used_resource (struct acpiphp_bridge *bridge, struct pci_dev *dev)
327 struct pci_resource *res;
329 dbg("Device %s\n", dev->slot_name);
331 for (count = 0; address[count]; count++) { /* for 6 BARs */
332 pci_read_config_dword(dev, address[count], &bar);
334 if (!bar) /* This BAR is not implemented */
337 pci_write_config_dword(dev, address[count], 0xFFFFFFFF);
338 pci_read_config_dword(dev, address[count], &len);
340 if (len & PCI_BASE_ADDRESS_SPACE_IO) {
342 base = bar & 0xFFFFFFFC;
343 len = len & (PCI_BASE_ADDRESS_IO_MASK & 0xFFFF);
344 len = len & ~(len - 1);
346 dbg("BAR[%d] %08x - %08x (IO)\n", count, (u32)base, (u32)base + len - 1);
348 spin_lock(&bridge->res_lock);
349 res = acpiphp_get_resource_with_base(&bridge->io_head, base, len);
350 spin_unlock(&bridge->res_lock);
355 base = bar & 0xFFFFFFF0;
356 if (len & PCI_BASE_ADDRESS_MEM_PREFETCH) {
362 if (len & PCI_BASE_ADDRESS_MEM_TYPE_64) { /* takes up another dword */
363 dbg("prefetch mem 64\n");
366 dbg("BAR[%d] %08x - %08x (PMEM)\n", count, (u32)base, (u32)base + len - 1);
367 spin_lock(&bridge->res_lock);
368 res = acpiphp_get_resource_with_base(&bridge->p_mem_head, base, len);
369 spin_unlock(&bridge->res_lock);
378 if (len & PCI_BASE_ADDRESS_MEM_TYPE_64) {
379 /* takes up another dword */
383 dbg("BAR[%d] %08x - %08x (MEM)\n", count, (u32)base, (u32)base + len - 1);
384 spin_lock(&bridge->res_lock);
385 res = acpiphp_get_resource_with_base(&bridge->mem_head, base, len);
386 spin_unlock(&bridge->res_lock);
392 pci_write_config_dword(dev, address[count], bar);
399 /* detect_pci_resource_bus - subtract resource under pci_bus */
400 static void detect_used_resource_bus(struct acpiphp_bridge *bridge, struct pci_bus *bus)
405 list_for_each (l, &bus->devices) {
407 detect_used_resource(bridge, dev);
408 /* XXX recursive call */
409 if (dev->subordinate)
410 detect_used_resource_bus(bridge, dev->subordinate);
416 * acpiphp_detect_pci_resource - detect resources under bridge
417 * @bridge: detect all resources already used under this bridge
419 * collect all resources already allocated for all devices under a bridge.
421 int acpiphp_detect_pci_resource (struct acpiphp_bridge *bridge)
423 detect_used_resource_bus(bridge, bridge->pci_bus);
430 * acpiphp_init_slot_resource - gather resource usage information of a slot
431 * @slot: ACPI slot object to be checked, should have valid pci_dev member
433 * TBD: PCI-to-PCI bridge case
434 * use pci_dev->resource[]
436 int acpiphp_init_func_resource (struct acpiphp_func *func)
450 struct pci_resource *res;
454 dbg("Hot-pluggable device %s\n", dev->slot_name);
456 for (count = 0; address[count]; count++) { /* for 6 BARs */
457 pci_read_config_dword(dev, address[count], &bar);
459 if (!bar) /* This BAR is not implemented */
462 pci_write_config_dword(dev, address[count], 0xFFFFFFFF);
463 pci_read_config_dword(dev, address[count], &len);
465 if (len & PCI_BASE_ADDRESS_SPACE_IO) {
467 base = bar & 0xFFFFFFFC;
468 len = len & (PCI_BASE_ADDRESS_IO_MASK & 0xFFFF);
469 len = len & ~(len - 1);
471 dbg("BAR[%d] %08x - %08x (IO)\n", count, (u32)base, (u32)base + len - 1);
473 res = acpiphp_make_resource(base, len);
477 res->next = func->io_head;
482 base = bar & 0xFFFFFFF0;
483 if (len & PCI_BASE_ADDRESS_MEM_PREFETCH) {
489 if (len & PCI_BASE_ADDRESS_MEM_TYPE_64) { /* takes up another dword */
490 dbg("prefetch mem 64\n");
493 dbg("BAR[%d] %08x - %08x (PMEM)\n", count, (u32)base, (u32)base + len - 1);
494 res = acpiphp_make_resource(base, len);
498 res->next = func->p_mem_head;
499 func->p_mem_head = res;
507 if (len & PCI_BASE_ADDRESS_MEM_TYPE_64) {
508 /* takes up another dword */
512 dbg("BAR[%d] %08x - %08x (MEM)\n", count, (u32)base, (u32)base + len - 1);
513 res = acpiphp_make_resource(base, len);
517 res->next = func->mem_head;
518 func->mem_head = res;
523 pci_write_config_dword(dev, address[count], bar);
526 acpiphp_dump_func_resource(func);
532 err("out of memory\n");
533 acpiphp_free_resource(&func->io_head);
534 acpiphp_free_resource(&func->mem_head);
535 acpiphp_free_resource(&func->p_mem_head);
542 * acpiphp_configure_slot - allocate PCI resources
543 * @slot: slot to be configured
545 * initializes a PCI functions on a device inserted
549 int acpiphp_configure_slot (struct acpiphp_slot *slot)
551 struct acpiphp_func *func;
558 pci_bus_read_config_byte(slot->bridge->pci_bus,
559 PCI_DEVFN(slot->device, 0),
560 PCI_HEADER_TYPE, &hdr);
565 list_for_each (l, &slot->funcs) {
566 func = list_entry(l, struct acpiphp_func, sibling);
567 if (is_multi || func->function == 0) {
568 pci_bus_read_config_dword(slot->bridge->pci_bus,
569 PCI_DEVFN(slot->device,
571 PCI_VENDOR_ID, &dvid);
572 if (dvid != 0xffffffff) {
573 retval = init_config_space(func);
584 /* for pci_visit_dev() */
585 static struct pci_visit configure_functions = {
586 .post_visit_pci_dev = configure_pci_dev
589 static struct pci_visit unconfigure_functions_phase1 = {
590 .post_visit_pci_dev = unconfigure_pci_dev_driver
593 static struct pci_visit unconfigure_functions_phase2 = {
594 .post_visit_pci_bus = unconfigure_pci_bus,
595 .post_visit_pci_dev = unconfigure_pci_dev
600 * acpiphp_configure_function - configure PCI function
601 * @func: function to be configured
603 * initializes a PCI functions on a device inserted
607 int acpiphp_configure_function (struct acpiphp_func *func)
610 struct pci_dev_wrapped wrapped_dev;
611 struct pci_bus_wrapped wrapped_bus;
612 struct acpiphp_bridge *bridge;
614 /* if pci_dev is NULL, ignore it */
618 bridge = func->slot->bridge;
620 memset(&wrapped_dev, 0, sizeof(struct pci_dev_wrapped));
621 memset(&wrapped_bus, 0, sizeof(struct pci_bus_wrapped));
622 wrapped_dev.dev = func->pci_dev;
623 wrapped_dev.data = func;
624 wrapped_bus.bus = bridge->pci_bus;
625 wrapped_bus.data = bridge;
627 retval = pci_visit_dev(&configure_functions, &wrapped_dev, &wrapped_bus);
637 * acpiphp_unconfigure_function - unconfigure PCI function
638 * @func: function to be unconfigured
641 int acpiphp_unconfigure_function (struct acpiphp_func *func)
643 struct acpiphp_bridge *bridge;
644 struct pci_dev_wrapped wrapped_dev;
645 struct pci_bus_wrapped wrapped_bus;
648 /* if pci_dev is NULL, ignore it */
652 memset(&wrapped_dev, 0, sizeof(struct pci_dev_wrapped));
653 memset(&wrapped_bus, 0, sizeof(struct pci_bus_wrapped));
654 wrapped_dev.dev = func->pci_dev;
655 //wrapped_dev.data = func;
656 wrapped_bus.bus = func->slot->bridge->pci_bus;
657 //wrapped_bus.data = func->slot->bridge;
659 retval = pci_visit_dev(&unconfigure_functions_phase1, &wrapped_dev, &wrapped_bus);
663 retval = pci_visit_dev(&unconfigure_functions_phase2, &wrapped_dev, &wrapped_bus);
667 /* free all resources */
668 bridge = func->slot->bridge;
670 spin_lock(&bridge->res_lock);
671 acpiphp_move_resource(&func->io_head, &bridge->io_head);
672 acpiphp_move_resource(&func->mem_head, &bridge->mem_head);
673 acpiphp_move_resource(&func->p_mem_head, &bridge->p_mem_head);
674 acpiphp_move_resource(&func->bus_head, &bridge->bus_head);
675 spin_unlock(&bridge->res_lock);
683 * acpiphp_configure_irq - configure PCI_INTERRUPT_PIN
685 * for x86 platforms, pcibios_enable_device calls pcibios_enable_irq,
686 * which allocates irq for pci_dev
688 * for IA64 platforms, we have to program dev->irq from pci IRQ routing
689 * information derived from ACPI table
692 * separate architecture dependent part
693 * (preferably, pci_enable_device() cares for allocating irq...)
695 static void acpiphp_configure_irq (struct pci_dev *dev)
697 #if CONFIG_IA64 /* XXX IA64 specific */
698 extern void iosapic_fixup_pci_interrupt (struct pci_dev *dev);
700 iosapic_fixup_pci_interrupt(dev);
701 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);