acpiphp_glue.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. /*
  2. * ACPI PCI HotPlug glue functions to ACPI CA subsystem
  3. *
  4. * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
  5. * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
  6. * Copyright (C) 2002,2003 NEC Corporation
  7. * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
  8. * Copyright (C) 2003-2005 Hewlett Packard
  9. * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
  10. * Copyright (C) 2005 Intel Corporation
  11. *
  12. * All rights reserved.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or (at
  17. * your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  22. * NON INFRINGEMENT. See the GNU General Public License for more
  23. * details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. *
  29. * Send feedback to <kristen.c.accardi@intel.com>
  30. *
  31. */
  32. /*
  33. * Lifetime rules for pci_dev:
  34. * - The one in acpiphp_func has its refcount elevated by pci_get_slot()
  35. * when the driver is loaded or when an insertion event occurs. It loses
  36. * a refcount when its ejected or the driver unloads.
  37. * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
  38. * when the bridge is scanned and it loses a refcount when the bridge
  39. * is removed.
  40. * - When a P2P bridge is present, we elevate the refcount on the subordinate
  41. * bus. It loses the refcount when the the driver unloads.
  42. */
  43. #include <linux/init.h>
  44. #include <linux/module.h>
  45. #include <linux/kernel.h>
  46. #include <linux/pci.h>
  47. #include <linux/pci_hotplug.h>
  48. #include <linux/pci-acpi.h>
  49. #include <linux/mutex.h>
  50. #include "../pci.h"
  51. #include "acpiphp.h"
  52. static LIST_HEAD(bridge_list);
  53. static LIST_HEAD(ioapic_list);
  54. static DEFINE_SPINLOCK(ioapic_list_lock);
  55. #define MY_NAME "acpiphp_glue"
  56. static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
  57. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  58. static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
  59. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
  60. /* callback routine to check for the existence of a pci dock device */
  61. static acpi_status
  62. is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
  63. {
  64. int *count = (int *)context;
  65. if (is_dock_device(handle)) {
  66. (*count)++;
  67. return AE_CTRL_TERMINATE;
  68. } else {
  69. return AE_OK;
  70. }
  71. }
  72. /*
  73. * the _DCK method can do funny things... and sometimes not
  74. * hah-hah funny.
  75. *
  76. * TBD - figure out a way to only call fixups for
  77. * systems that require them.
  78. */
  79. static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
  80. void *v)
  81. {
  82. struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
  83. struct pci_bus *bus = func->slot->bridge->pci_bus;
  84. u32 buses;
  85. if (!bus->self)
  86. return NOTIFY_OK;
  87. /* fixup bad _DCK function that rewrites
  88. * secondary bridge on slot
  89. */
  90. pci_read_config_dword(bus->self,
  91. PCI_PRIMARY_BUS,
  92. &buses);
  93. if (((buses >> 8) & 0xff) != bus->secondary) {
  94. buses = (buses & 0xff000000)
  95. | ((unsigned int)(bus->primary) << 0)
  96. | ((unsigned int)(bus->secondary) << 8)
  97. | ((unsigned int)(bus->subordinate) << 16);
  98. pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
  99. }
  100. return NOTIFY_OK;
  101. }
  102. static struct acpi_dock_ops acpiphp_dock_ops = {
  103. .handler = handle_hotplug_event_func,
  104. };
  105. /* callback routine to register each ACPI PCI slot object */
  106. static acpi_status
  107. register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  108. {
  109. struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
  110. struct acpiphp_slot *slot;
  111. struct acpiphp_func *newfunc;
  112. acpi_handle tmp;
  113. acpi_status status = AE_OK;
  114. unsigned long long adr, sun;
  115. int device, function, retval;
  116. struct pci_bus *pbus = bridge->pci_bus;
  117. if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
  118. return AE_OK;
  119. acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  120. device = (adr >> 16) & 0xffff;
  121. function = adr & 0xffff;
  122. newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
  123. if (!newfunc)
  124. return AE_NO_MEMORY;
  125. INIT_LIST_HEAD(&newfunc->sibling);
  126. newfunc->handle = handle;
  127. newfunc->function = function;
  128. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
  129. newfunc->flags = FUNC_HAS_EJ0;
  130. if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
  131. newfunc->flags |= FUNC_HAS_STA;
  132. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
  133. newfunc->flags |= FUNC_HAS_PS0;
  134. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
  135. newfunc->flags |= FUNC_HAS_PS3;
  136. if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
  137. newfunc->flags |= FUNC_HAS_DCK;
  138. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  139. if (ACPI_FAILURE(status)) {
  140. /*
  141. * use the count of the number of slots we've found
  142. * for the number of the slot
  143. */
  144. sun = bridge->nr_slots+1;
  145. }
  146. /* search for objects that share the same slot */
  147. for (slot = bridge->slots; slot; slot = slot->next)
  148. if (slot->device == device) {
  149. if (slot->sun != sun)
  150. warn("sibling found, but _SUN doesn't match!\n");
  151. break;
  152. }
  153. if (!slot) {
  154. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  155. if (!slot) {
  156. kfree(newfunc);
  157. return AE_NO_MEMORY;
  158. }
  159. slot->bridge = bridge;
  160. slot->device = device;
  161. slot->sun = sun;
  162. INIT_LIST_HEAD(&slot->funcs);
  163. mutex_init(&slot->crit_sect);
  164. slot->next = bridge->slots;
  165. bridge->slots = slot;
  166. bridge->nr_slots++;
  167. dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  168. slot->sun, pci_domain_nr(pbus), pbus->number, device);
  169. retval = acpiphp_register_hotplug_slot(slot);
  170. if (retval) {
  171. if (retval == -EBUSY)
  172. warn("Slot %llu already registered by another "
  173. "hotplug driver\n", slot->sun);
  174. else
  175. warn("acpiphp_register_hotplug_slot failed "
  176. "(err code = 0x%x)\n", retval);
  177. goto err_exit;
  178. }
  179. }
  180. newfunc->slot = slot;
  181. list_add_tail(&newfunc->sibling, &slot->funcs);
  182. /* associate corresponding pci_dev */
  183. newfunc->pci_dev = pci_get_slot(pbus, PCI_DEVFN(device, function));
  184. if (newfunc->pci_dev) {
  185. slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
  186. }
  187. if (is_dock_device(handle)) {
  188. /* we don't want to call this device's _EJ0
  189. * because we want the dock notify handler
  190. * to call it after it calls _DCK
  191. */
  192. newfunc->flags &= ~FUNC_HAS_EJ0;
  193. if (register_hotplug_dock_device(handle,
  194. &acpiphp_dock_ops, newfunc))
  195. dbg("failed to register dock device\n");
  196. /* we need to be notified when dock events happen
  197. * outside of the hotplug operation, since we may
  198. * need to do fixups before we can hotplug.
  199. */
  200. newfunc->nb.notifier_call = post_dock_fixups;
  201. if (register_dock_notifier(&newfunc->nb))
  202. dbg("failed to register a dock notifier");
  203. }
  204. /* install notify handler */
  205. if (!(newfunc->flags & FUNC_HAS_DCK)) {
  206. status = acpi_install_notify_handler(handle,
  207. ACPI_SYSTEM_NOTIFY,
  208. handle_hotplug_event_func,
  209. newfunc);
  210. if (ACPI_FAILURE(status))
  211. err("failed to register interrupt notify handler\n");
  212. } else
  213. status = AE_OK;
  214. return status;
  215. err_exit:
  216. bridge->nr_slots--;
  217. bridge->slots = slot->next;
  218. kfree(slot);
  219. kfree(newfunc);
  220. return AE_OK;
  221. }
  222. /* see if it's worth looking at this bridge */
  223. static int detect_ejectable_slots(struct pci_bus *pbus)
  224. {
  225. int found = acpi_pci_detect_ejectable(pbus);
  226. if (!found) {
  227. acpi_handle bridge_handle = acpi_pci_get_bridge_handle(pbus);
  228. if (!bridge_handle)
  229. return 0;
  230. acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
  231. is_pci_dock_device, (void *)&found, NULL);
  232. }
  233. return found;
  234. }
  235. /* decode ACPI 2.0 _HPP hot plug parameters */
  236. static void decode_hpp(struct acpiphp_bridge *bridge)
  237. {
  238. acpi_status status;
  239. status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
  240. if (ACPI_FAILURE(status) ||
  241. !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
  242. /* use default numbers */
  243. printk(KERN_WARNING
  244. "%s: Could not get hotplug parameters. Use defaults\n",
  245. __func__);
  246. bridge->hpp.t0 = &bridge->hpp.type0_data;
  247. bridge->hpp.t0->revision = 0;
  248. bridge->hpp.t0->cache_line_size = 0x10;
  249. bridge->hpp.t0->latency_timer = 0x40;
  250. bridge->hpp.t0->enable_serr = 0;
  251. bridge->hpp.t0->enable_perr = 0;
  252. }
  253. }
  254. /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
  255. static void init_bridge_misc(struct acpiphp_bridge *bridge)
  256. {
  257. acpi_status status;
  258. /* decode ACPI 2.0 _HPP (hot plug parameters) */
  259. decode_hpp(bridge);
  260. /* must be added to the list prior to calling register_slot */
  261. list_add(&bridge->list, &bridge_list);
  262. /* register all slot objects under this bridge */
  263. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
  264. register_slot, bridge, NULL);
  265. if (ACPI_FAILURE(status)) {
  266. list_del(&bridge->list);
  267. return;
  268. }
  269. /* install notify handler */
  270. if (bridge->type != BRIDGE_TYPE_HOST) {
  271. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  272. status = acpi_remove_notify_handler(bridge->func->handle,
  273. ACPI_SYSTEM_NOTIFY,
  274. handle_hotplug_event_func);
  275. if (ACPI_FAILURE(status))
  276. err("failed to remove notify handler\n");
  277. }
  278. status = acpi_install_notify_handler(bridge->handle,
  279. ACPI_SYSTEM_NOTIFY,
  280. handle_hotplug_event_bridge,
  281. bridge);
  282. if (ACPI_FAILURE(status)) {
  283. err("failed to register interrupt notify handler\n");
  284. }
  285. }
  286. }
  287. /* find acpiphp_func from acpiphp_bridge */
  288. static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
  289. {
  290. struct list_head *node, *l;
  291. struct acpiphp_bridge *bridge;
  292. struct acpiphp_slot *slot;
  293. struct acpiphp_func *func;
  294. list_for_each(node, &bridge_list) {
  295. bridge = list_entry(node, struct acpiphp_bridge, list);
  296. for (slot = bridge->slots; slot; slot = slot->next) {
  297. list_for_each(l, &slot->funcs) {
  298. func = list_entry(l, struct acpiphp_func,
  299. sibling);
  300. if (func->handle == handle)
  301. return func;
  302. }
  303. }
  304. }
  305. return NULL;
  306. }
  307. static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
  308. {
  309. acpi_handle dummy_handle;
  310. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  311. "_STA", &dummy_handle)))
  312. bridge->flags |= BRIDGE_HAS_STA;
  313. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  314. "_EJ0", &dummy_handle)))
  315. bridge->flags |= BRIDGE_HAS_EJ0;
  316. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  317. "_PS0", &dummy_handle)))
  318. bridge->flags |= BRIDGE_HAS_PS0;
  319. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  320. "_PS3", &dummy_handle)))
  321. bridge->flags |= BRIDGE_HAS_PS3;
  322. /* is this ejectable p2p bridge? */
  323. if (bridge->flags & BRIDGE_HAS_EJ0) {
  324. struct acpiphp_func *func;
  325. dbg("found ejectable p2p bridge\n");
  326. /* make link between PCI bridge and PCI function */
  327. func = acpiphp_bridge_handle_to_function(bridge->handle);
  328. if (!func)
  329. return;
  330. bridge->func = func;
  331. func->bridge = bridge;
  332. }
  333. }
  334. /* allocate and initialize host bridge data structure */
  335. static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
  336. {
  337. struct acpiphp_bridge *bridge;
  338. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  339. if (bridge == NULL)
  340. return;
  341. bridge->type = BRIDGE_TYPE_HOST;
  342. bridge->handle = handle;
  343. bridge->pci_bus = pci_bus;
  344. spin_lock_init(&bridge->res_lock);
  345. init_bridge_misc(bridge);
  346. }
  347. /* allocate and initialize PCI-to-PCI bridge data structure */
  348. static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
  349. {
  350. struct acpiphp_bridge *bridge;
  351. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  352. if (bridge == NULL) {
  353. err("out of memory\n");
  354. return;
  355. }
  356. bridge->type = BRIDGE_TYPE_P2P;
  357. bridge->handle = handle;
  358. config_p2p_bridge_flags(bridge);
  359. bridge->pci_dev = pci_dev_get(pci_dev);
  360. bridge->pci_bus = pci_dev->subordinate;
  361. if (!bridge->pci_bus) {
  362. err("This is not a PCI-to-PCI bridge!\n");
  363. goto err;
  364. }
  365. /*
  366. * Grab a ref to the subordinate PCI bus in case the bus is
  367. * removed via PCI core logical hotplug. The ref pins the bus
  368. * (which we access during module unload).
  369. */
  370. get_device(&bridge->pci_bus->dev);
  371. spin_lock_init(&bridge->res_lock);
  372. init_bridge_misc(bridge);
  373. return;
  374. err:
  375. pci_dev_put(pci_dev);
  376. kfree(bridge);
  377. return;
  378. }
  379. /* callback routine to find P2P bridges */
  380. static acpi_status
  381. find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  382. {
  383. acpi_status status;
  384. acpi_handle dummy_handle;
  385. unsigned long long tmp;
  386. int device, function;
  387. struct pci_dev *dev;
  388. struct pci_bus *pci_bus = context;
  389. status = acpi_get_handle(handle, "_ADR", &dummy_handle);
  390. if (ACPI_FAILURE(status))
  391. return AE_OK; /* continue */
  392. status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
  393. if (ACPI_FAILURE(status)) {
  394. dbg("%s: _ADR evaluation failure\n", __func__);
  395. return AE_OK;
  396. }
  397. device = (tmp >> 16) & 0xffff;
  398. function = tmp & 0xffff;
  399. dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
  400. if (!dev || !dev->subordinate)
  401. goto out;
  402. /* check if this bridge has ejectable slots */
  403. if ((detect_ejectable_slots(dev->subordinate) > 0)) {
  404. dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
  405. add_p2p_bridge(handle, dev);
  406. }
  407. /* search P2P bridges under this p2p bridge */
  408. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  409. find_p2p_bridge, dev->subordinate, NULL);
  410. if (ACPI_FAILURE(status))
  411. warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
  412. out:
  413. pci_dev_put(dev);
  414. return AE_OK;
  415. }
  416. /* find hot-pluggable slots, and then find P2P bridge */
  417. static int add_bridge(acpi_handle handle)
  418. {
  419. acpi_status status;
  420. unsigned long long tmp;
  421. int seg, bus;
  422. acpi_handle dummy_handle;
  423. struct pci_bus *pci_bus;
  424. /* if the bridge doesn't have _STA, we assume it is always there */
  425. status = acpi_get_handle(handle, "_STA", &dummy_handle);
  426. if (ACPI_SUCCESS(status)) {
  427. status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
  428. if (ACPI_FAILURE(status)) {
  429. dbg("%s: _STA evaluation failure\n", __func__);
  430. return 0;
  431. }
  432. if ((tmp & ACPI_STA_FUNCTIONING) == 0)
  433. /* don't register this object */
  434. return 0;
  435. }
  436. /* get PCI segment number */
  437. status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
  438. seg = ACPI_SUCCESS(status) ? tmp : 0;
  439. /* get PCI bus number */
  440. status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
  441. if (ACPI_SUCCESS(status)) {
  442. bus = tmp;
  443. } else {
  444. warn("can't get bus number, assuming 0\n");
  445. bus = 0;
  446. }
  447. pci_bus = pci_find_bus(seg, bus);
  448. if (!pci_bus) {
  449. err("Can't find bus %04x:%02x\n", seg, bus);
  450. return 0;
  451. }
  452. /* check if this bridge has ejectable slots */
  453. if (detect_ejectable_slots(pci_bus) > 0) {
  454. dbg("found PCI host-bus bridge with hot-pluggable slots\n");
  455. add_host_bridge(handle, pci_bus);
  456. }
  457. /* search P2P bridges under this host bridge */
  458. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  459. find_p2p_bridge, pci_bus, NULL);
  460. if (ACPI_FAILURE(status))
  461. warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
  462. return 0;
  463. }
  464. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  465. {
  466. struct list_head *head;
  467. list_for_each(head, &bridge_list) {
  468. struct acpiphp_bridge *bridge = list_entry(head,
  469. struct acpiphp_bridge, list);
  470. if (bridge->handle == handle)
  471. return bridge;
  472. }
  473. return NULL;
  474. }
  475. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  476. {
  477. struct list_head *list, *tmp;
  478. struct acpiphp_slot *slot;
  479. acpi_status status;
  480. acpi_handle handle = bridge->handle;
  481. status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  482. handle_hotplug_event_bridge);
  483. if (ACPI_FAILURE(status))
  484. err("failed to remove notify handler\n");
  485. if ((bridge->type != BRIDGE_TYPE_HOST) &&
  486. ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
  487. status = acpi_install_notify_handler(bridge->func->handle,
  488. ACPI_SYSTEM_NOTIFY,
  489. handle_hotplug_event_func,
  490. bridge->func);
  491. if (ACPI_FAILURE(status))
  492. err("failed to install interrupt notify handler\n");
  493. }
  494. slot = bridge->slots;
  495. while (slot) {
  496. struct acpiphp_slot *next = slot->next;
  497. list_for_each_safe (list, tmp, &slot->funcs) {
  498. struct acpiphp_func *func;
  499. func = list_entry(list, struct acpiphp_func, sibling);
  500. if (is_dock_device(func->handle)) {
  501. unregister_hotplug_dock_device(func->handle);
  502. unregister_dock_notifier(&func->nb);
  503. }
  504. if (!(func->flags & FUNC_HAS_DCK)) {
  505. status = acpi_remove_notify_handler(func->handle,
  506. ACPI_SYSTEM_NOTIFY,
  507. handle_hotplug_event_func);
  508. if (ACPI_FAILURE(status))
  509. err("failed to remove notify handler\n");
  510. }
  511. pci_dev_put(func->pci_dev);
  512. list_del(list);
  513. kfree(func);
  514. }
  515. acpiphp_unregister_hotplug_slot(slot);
  516. list_del(&slot->funcs);
  517. kfree(slot);
  518. slot = next;
  519. }
  520. /*
  521. * Only P2P bridges have a pci_dev
  522. */
  523. if (bridge->pci_dev)
  524. put_device(&bridge->pci_bus->dev);
  525. pci_dev_put(bridge->pci_dev);
  526. list_del(&bridge->list);
  527. kfree(bridge);
  528. }
  529. static acpi_status
  530. cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  531. {
  532. struct acpiphp_bridge *bridge;
  533. /* cleanup p2p bridges under this P2P bridge
  534. in a depth-first manner */
  535. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  536. cleanup_p2p_bridge, NULL, NULL);
  537. bridge = acpiphp_handle_to_bridge(handle);
  538. if (bridge)
  539. cleanup_bridge(bridge);
  540. return AE_OK;
  541. }
  542. static void remove_bridge(acpi_handle handle)
  543. {
  544. struct acpiphp_bridge *bridge;
  545. /* cleanup p2p bridges under this host bridge
  546. in a depth-first manner */
  547. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  548. (u32)1, cleanup_p2p_bridge, NULL, NULL);
  549. /*
  550. * On root bridges with hotplug slots directly underneath (ie,
  551. * no p2p bridge inbetween), we call cleanup_bridge().
  552. *
  553. * The else clause cleans up root bridges that either had no
  554. * hotplug slots at all, or had a p2p bridge underneath.
  555. */
  556. bridge = acpiphp_handle_to_bridge(handle);
  557. if (bridge)
  558. cleanup_bridge(bridge);
  559. else
  560. acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  561. handle_hotplug_event_bridge);
  562. }
  563. static struct pci_dev * get_apic_pci_info(acpi_handle handle)
  564. {
  565. struct acpi_pci_id id;
  566. struct pci_bus *bus;
  567. struct pci_dev *dev;
  568. if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
  569. return NULL;
  570. bus = pci_find_bus(id.segment, id.bus);
  571. if (!bus)
  572. return NULL;
  573. dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
  574. if (!dev)
  575. return NULL;
  576. if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
  577. (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
  578. {
  579. pci_dev_put(dev);
  580. return NULL;
  581. }
  582. return dev;
  583. }
  584. static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
  585. {
  586. acpi_status status;
  587. int result = -1;
  588. unsigned long long gsb;
  589. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  590. union acpi_object *obj;
  591. void *table;
  592. status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
  593. if (ACPI_SUCCESS(status)) {
  594. *gsi_base = (u32)gsb;
  595. return 0;
  596. }
  597. status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
  598. if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
  599. return -1;
  600. obj = buffer.pointer;
  601. if (obj->type != ACPI_TYPE_BUFFER)
  602. goto out;
  603. table = obj->buffer.pointer;
  604. switch (((struct acpi_subtable_header *)table)->type) {
  605. case ACPI_MADT_TYPE_IO_SAPIC:
  606. *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
  607. result = 0;
  608. break;
  609. case ACPI_MADT_TYPE_IO_APIC:
  610. *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
  611. result = 0;
  612. break;
  613. default:
  614. break;
  615. }
  616. out:
  617. kfree(buffer.pointer);
  618. return result;
  619. }
  620. static acpi_status
  621. ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
  622. {
  623. acpi_status status;
  624. unsigned long long sta;
  625. acpi_handle tmp;
  626. struct pci_dev *pdev;
  627. u32 gsi_base;
  628. u64 phys_addr;
  629. struct acpiphp_ioapic *ioapic;
  630. /* Evaluate _STA if present */
  631. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  632. if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
  633. return AE_CTRL_DEPTH;
  634. /* Scan only PCI bus scope */
  635. status = acpi_get_handle(handle, "_HID", &tmp);
  636. if (ACPI_SUCCESS(status))
  637. return AE_CTRL_DEPTH;
  638. if (get_gsi_base(handle, &gsi_base))
  639. return AE_OK;
  640. ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
  641. if (!ioapic)
  642. return AE_NO_MEMORY;
  643. pdev = get_apic_pci_info(handle);
  644. if (!pdev)
  645. goto exit_kfree;
  646. if (pci_enable_device(pdev))
  647. goto exit_pci_dev_put;
  648. pci_set_master(pdev);
  649. if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
  650. goto exit_pci_disable_device;
  651. phys_addr = pci_resource_start(pdev, 0);
  652. if (acpi_register_ioapic(handle, phys_addr, gsi_base))
  653. goto exit_pci_release_region;
  654. ioapic->gsi_base = gsi_base;
  655. ioapic->dev = pdev;
  656. spin_lock(&ioapic_list_lock);
  657. list_add_tail(&ioapic->list, &ioapic_list);
  658. spin_unlock(&ioapic_list_lock);
  659. return AE_OK;
  660. exit_pci_release_region:
  661. pci_release_region(pdev, 0);
  662. exit_pci_disable_device:
  663. pci_disable_device(pdev);
  664. exit_pci_dev_put:
  665. pci_dev_put(pdev);
  666. exit_kfree:
  667. kfree(ioapic);
  668. return AE_OK;
  669. }
  670. static acpi_status
  671. ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
  672. {
  673. acpi_status status;
  674. unsigned long long sta;
  675. acpi_handle tmp;
  676. u32 gsi_base;
  677. struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
  678. /* Evaluate _STA if present */
  679. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  680. if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
  681. return AE_CTRL_DEPTH;
  682. /* Scan only PCI bus scope */
  683. status = acpi_get_handle(handle, "_HID", &tmp);
  684. if (ACPI_SUCCESS(status))
  685. return AE_CTRL_DEPTH;
  686. if (get_gsi_base(handle, &gsi_base))
  687. return AE_OK;
  688. acpi_unregister_ioapic(handle, gsi_base);
  689. spin_lock(&ioapic_list_lock);
  690. list_for_each_entry_safe(pos, n, &ioapic_list, list) {
  691. if (pos->gsi_base != gsi_base)
  692. continue;
  693. ioapic = pos;
  694. list_del(&ioapic->list);
  695. break;
  696. }
  697. spin_unlock(&ioapic_list_lock);
  698. if (!ioapic)
  699. return AE_OK;
  700. pci_release_region(ioapic->dev, 0);
  701. pci_disable_device(ioapic->dev);
  702. pci_dev_put(ioapic->dev);
  703. kfree(ioapic);
  704. return AE_OK;
  705. }
  706. static int acpiphp_configure_ioapics(acpi_handle handle)
  707. {
  708. ioapic_add(handle, 0, NULL, NULL);
  709. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  710. ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
  711. return 0;
  712. }
  713. static int acpiphp_unconfigure_ioapics(acpi_handle handle)
  714. {
  715. ioapic_remove(handle, 0, NULL, NULL);
  716. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  717. ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
  718. return 0;
  719. }
  720. static int power_on_slot(struct acpiphp_slot *slot)
  721. {
  722. acpi_status status;
  723. struct acpiphp_func *func;
  724. struct list_head *l;
  725. int retval = 0;
  726. /* if already enabled, just skip */
  727. if (slot->flags & SLOT_POWEREDON)
  728. goto err_exit;
  729. list_for_each (l, &slot->funcs) {
  730. func = list_entry(l, struct acpiphp_func, sibling);
  731. if (func->flags & FUNC_HAS_PS0) {
  732. dbg("%s: executing _PS0\n", __func__);
  733. status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
  734. if (ACPI_FAILURE(status)) {
  735. warn("%s: _PS0 failed\n", __func__);
  736. retval = -1;
  737. goto err_exit;
  738. } else
  739. break;
  740. }
  741. }
  742. /* TBD: evaluate _STA to check if the slot is enabled */
  743. slot->flags |= SLOT_POWEREDON;
  744. err_exit:
  745. return retval;
  746. }
  747. static int power_off_slot(struct acpiphp_slot *slot)
  748. {
  749. acpi_status status;
  750. struct acpiphp_func *func;
  751. struct list_head *l;
  752. int retval = 0;
  753. /* if already disabled, just skip */
  754. if ((slot->flags & SLOT_POWEREDON) == 0)
  755. goto err_exit;
  756. list_for_each (l, &slot->funcs) {
  757. func = list_entry(l, struct acpiphp_func, sibling);
  758. if (func->flags & FUNC_HAS_PS3) {
  759. status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
  760. if (ACPI_FAILURE(status)) {
  761. warn("%s: _PS3 failed\n", __func__);
  762. retval = -1;
  763. goto err_exit;
  764. } else
  765. break;
  766. }
  767. }
  768. /* TBD: evaluate _STA to check if the slot is disabled */
  769. slot->flags &= (~SLOT_POWEREDON);
  770. err_exit:
  771. return retval;
  772. }
  773. /**
  774. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  775. * @bus: bus to start search with
  776. */
  777. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  778. {
  779. struct list_head *tmp;
  780. unsigned char max, n;
  781. /*
  782. * pci_bus_max_busnr will return the highest
  783. * reserved busnr for all these children.
  784. * that is equivalent to the bus->subordinate
  785. * value. We don't want to use the parent's
  786. * bus->subordinate value because it could have
  787. * padding in it.
  788. */
  789. max = bus->secondary;
  790. list_for_each(tmp, &bus->children) {
  791. n = pci_bus_max_busnr(pci_bus_b(tmp));
  792. if (n > max)
  793. max = n;
  794. }
  795. return max;
  796. }
  797. /**
  798. * acpiphp_bus_add - add a new bus to acpi subsystem
  799. * @func: acpiphp_func of the bridge
  800. */
  801. static int acpiphp_bus_add(struct acpiphp_func *func)
  802. {
  803. acpi_handle phandle;
  804. struct acpi_device *device, *pdevice;
  805. int ret_val;
  806. acpi_get_parent(func->handle, &phandle);
  807. if (acpi_bus_get_device(phandle, &pdevice)) {
  808. dbg("no parent device, assuming NULL\n");
  809. pdevice = NULL;
  810. }
  811. if (!acpi_bus_get_device(func->handle, &device)) {
  812. dbg("bus exists... trim\n");
  813. /* this shouldn't be in here, so remove
  814. * the bus then re-add it...
  815. */
  816. ret_val = acpi_bus_trim(device, 1);
  817. dbg("acpi_bus_trim return %x\n", ret_val);
  818. }
  819. ret_val = acpi_bus_add(&device, pdevice, func->handle,
  820. ACPI_BUS_TYPE_DEVICE);
  821. if (ret_val) {
  822. dbg("error adding bus, %x\n",
  823. -ret_val);
  824. goto acpiphp_bus_add_out;
  825. }
  826. /*
  827. * try to start anyway. We could have failed to add
  828. * simply because this bus had previously been added
  829. * on another add. Don't bother with the return value
  830. * we just keep going.
  831. */
  832. ret_val = acpi_bus_start(device);
  833. acpiphp_bus_add_out:
  834. return ret_val;
  835. }
  836. /**
  837. * acpiphp_bus_trim - trim a bus from acpi subsystem
  838. * @handle: handle to acpi namespace
  839. */
  840. static int acpiphp_bus_trim(acpi_handle handle)
  841. {
  842. struct acpi_device *device;
  843. int retval;
  844. retval = acpi_bus_get_device(handle, &device);
  845. if (retval) {
  846. dbg("acpi_device not found\n");
  847. return retval;
  848. }
  849. retval = acpi_bus_trim(device, 1);
  850. if (retval)
  851. err("cannot remove from acpi list\n");
  852. return retval;
  853. }
  854. /**
  855. * enable_device - enable, configure a slot
  856. * @slot: slot to be enabled
  857. *
  858. * This function should be called per *physical slot*,
  859. * not per each slot object in ACPI namespace.
  860. */
  861. static int __ref enable_device(struct acpiphp_slot *slot)
  862. {
  863. struct pci_dev *dev;
  864. struct pci_bus *bus = slot->bridge->pci_bus;
  865. struct list_head *l;
  866. struct acpiphp_func *func;
  867. int retval = 0;
  868. int num, max, pass;
  869. acpi_status status;
  870. if (slot->flags & SLOT_ENABLED)
  871. goto err_exit;
  872. /* sanity check: dev should be NULL when hot-plugged in */
  873. dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
  874. if (dev) {
  875. /* This case shouldn't happen */
  876. err("pci_dev structure already exists.\n");
  877. pci_dev_put(dev);
  878. retval = -1;
  879. goto err_exit;
  880. }
  881. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  882. if (num == 0) {
  883. err("No new device found\n");
  884. retval = -1;
  885. goto err_exit;
  886. }
  887. max = acpiphp_max_busnr(bus);
  888. for (pass = 0; pass < 2; pass++) {
  889. list_for_each_entry(dev, &bus->devices, bus_list) {
  890. if (PCI_SLOT(dev->devfn) != slot->device)
  891. continue;
  892. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  893. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  894. max = pci_scan_bridge(bus, dev, max, pass);
  895. if (pass && dev->subordinate)
  896. pci_bus_size_bridges(dev->subordinate);
  897. }
  898. }
  899. }
  900. list_for_each (l, &slot->funcs) {
  901. func = list_entry(l, struct acpiphp_func, sibling);
  902. acpiphp_bus_add(func);
  903. }
  904. pci_bus_assign_resources(bus);
  905. acpiphp_sanitize_bus(bus);
  906. acpiphp_set_hpp_values(slot->bridge->handle, bus);
  907. list_for_each_entry(func, &slot->funcs, sibling)
  908. acpiphp_configure_ioapics(func->handle);
  909. pci_enable_bridges(bus);
  910. pci_bus_add_devices(bus);
  911. /* associate pci_dev to our representation */
  912. list_for_each (l, &slot->funcs) {
  913. func = list_entry(l, struct acpiphp_func, sibling);
  914. func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  915. func->function));
  916. if (!func->pci_dev)
  917. continue;
  918. if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
  919. func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
  920. continue;
  921. status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
  922. if (ACPI_FAILURE(status))
  923. warn("find_p2p_bridge failed (error code = 0x%x)\n",
  924. status);
  925. }
  926. slot->flags |= SLOT_ENABLED;
  927. err_exit:
  928. return retval;
  929. }
  930. static void disable_bridges(struct pci_bus *bus)
  931. {
  932. struct pci_dev *dev;
  933. list_for_each_entry(dev, &bus->devices, bus_list) {
  934. if (dev->subordinate) {
  935. disable_bridges(dev->subordinate);
  936. pci_disable_device(dev);
  937. }
  938. }
  939. }
  940. /**
  941. * disable_device - disable a slot
  942. * @slot: ACPI PHP slot
  943. */
  944. static int disable_device(struct acpiphp_slot *slot)
  945. {
  946. int retval = 0;
  947. struct acpiphp_func *func;
  948. struct list_head *l;
  949. /* is this slot already disabled? */
  950. if (!(slot->flags & SLOT_ENABLED))
  951. goto err_exit;
  952. list_for_each (l, &slot->funcs) {
  953. func = list_entry(l, struct acpiphp_func, sibling);
  954. if (func->bridge) {
  955. /* cleanup p2p bridges under this P2P bridge */
  956. cleanup_p2p_bridge(func->bridge->handle,
  957. (u32)1, NULL, NULL);
  958. func->bridge = NULL;
  959. }
  960. if (func->pci_dev) {
  961. pci_stop_bus_device(func->pci_dev);
  962. if (func->pci_dev->subordinate) {
  963. disable_bridges(func->pci_dev->subordinate);
  964. pci_disable_device(func->pci_dev);
  965. }
  966. }
  967. }
  968. list_for_each (l, &slot->funcs) {
  969. func = list_entry(l, struct acpiphp_func, sibling);
  970. acpiphp_unconfigure_ioapics(func->handle);
  971. acpiphp_bus_trim(func->handle);
  972. /* try to remove anyway.
  973. * acpiphp_bus_add might have been failed */
  974. if (!func->pci_dev)
  975. continue;
  976. pci_remove_bus_device(func->pci_dev);
  977. pci_dev_put(func->pci_dev);
  978. func->pci_dev = NULL;
  979. }
  980. slot->flags &= (~SLOT_ENABLED);
  981. err_exit:
  982. return retval;
  983. }
  984. /**
  985. * get_slot_status - get ACPI slot status
  986. * @slot: ACPI PHP slot
  987. *
  988. * If a slot has _STA for each function and if any one of them
  989. * returned non-zero status, return it.
  990. *
  991. * If a slot doesn't have _STA and if any one of its functions'
  992. * configuration space is configured, return 0x0f as a _STA.
  993. *
  994. * Otherwise return 0.
  995. */
  996. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  997. {
  998. acpi_status status;
  999. unsigned long long sta = 0;
  1000. u32 dvid;
  1001. struct list_head *l;
  1002. struct acpiphp_func *func;
  1003. list_for_each (l, &slot->funcs) {
  1004. func = list_entry(l, struct acpiphp_func, sibling);
  1005. if (func->flags & FUNC_HAS_STA) {
  1006. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  1007. if (ACPI_SUCCESS(status) && sta)
  1008. break;
  1009. } else {
  1010. pci_bus_read_config_dword(slot->bridge->pci_bus,
  1011. PCI_DEVFN(slot->device,
  1012. func->function),
  1013. PCI_VENDOR_ID, &dvid);
  1014. if (dvid != 0xffffffff) {
  1015. sta = ACPI_STA_ALL;
  1016. break;
  1017. }
  1018. }
  1019. }
  1020. return (unsigned int)sta;
  1021. }
  1022. /**
  1023. * acpiphp_eject_slot - physically eject the slot
  1024. * @slot: ACPI PHP slot
  1025. */
  1026. int acpiphp_eject_slot(struct acpiphp_slot *slot)
  1027. {
  1028. acpi_status status;
  1029. struct acpiphp_func *func;
  1030. struct list_head *l;
  1031. struct acpi_object_list arg_list;
  1032. union acpi_object arg;
  1033. list_for_each (l, &slot->funcs) {
  1034. func = list_entry(l, struct acpiphp_func, sibling);
  1035. /* We don't want to call _EJ0 on non-existing functions. */
  1036. if ((func->flags & FUNC_HAS_EJ0)) {
  1037. /* _EJ0 method take one argument */
  1038. arg_list.count = 1;
  1039. arg_list.pointer = &arg;
  1040. arg.type = ACPI_TYPE_INTEGER;
  1041. arg.integer.value = 1;
  1042. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  1043. if (ACPI_FAILURE(status)) {
  1044. warn("%s: _EJ0 failed\n", __func__);
  1045. return -1;
  1046. } else
  1047. break;
  1048. }
  1049. }
  1050. return 0;
  1051. }
  1052. /**
  1053. * acpiphp_check_bridge - re-enumerate devices
  1054. * @bridge: where to begin re-enumeration
  1055. *
  1056. * Iterate over all slots under this bridge and make sure that if a
  1057. * card is present they are enabled, and if not they are disabled.
  1058. */
  1059. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  1060. {
  1061. struct acpiphp_slot *slot;
  1062. int retval = 0;
  1063. int enabled, disabled;
  1064. enabled = disabled = 0;
  1065. for (slot = bridge->slots; slot; slot = slot->next) {
  1066. unsigned int status = get_slot_status(slot);
  1067. if (slot->flags & SLOT_ENABLED) {
  1068. if (status == ACPI_STA_ALL)
  1069. continue;
  1070. retval = acpiphp_disable_slot(slot);
  1071. if (retval) {
  1072. err("Error occurred in disabling\n");
  1073. goto err_exit;
  1074. } else {
  1075. acpiphp_eject_slot(slot);
  1076. }
  1077. disabled++;
  1078. } else {
  1079. if (status != ACPI_STA_ALL)
  1080. continue;
  1081. retval = acpiphp_enable_slot(slot);
  1082. if (retval) {
  1083. err("Error occurred in enabling\n");
  1084. goto err_exit;
  1085. }
  1086. enabled++;
  1087. }
  1088. }
  1089. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  1090. err_exit:
  1091. return retval;
  1092. }
  1093. static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
  1094. {
  1095. u16 pci_cmd, pci_bctl;
  1096. struct pci_dev *cdev;
  1097. /* Program hpp values for this device */
  1098. if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
  1099. (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  1100. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
  1101. return;
  1102. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
  1103. return;
  1104. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
  1105. bridge->hpp.t0->cache_line_size);
  1106. pci_write_config_byte(dev, PCI_LATENCY_TIMER,
  1107. bridge->hpp.t0->latency_timer);
  1108. pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
  1109. if (bridge->hpp.t0->enable_serr)
  1110. pci_cmd |= PCI_COMMAND_SERR;
  1111. else
  1112. pci_cmd &= ~PCI_COMMAND_SERR;
  1113. if (bridge->hpp.t0->enable_perr)
  1114. pci_cmd |= PCI_COMMAND_PARITY;
  1115. else
  1116. pci_cmd &= ~PCI_COMMAND_PARITY;
  1117. pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
  1118. /* Program bridge control value and child devices */
  1119. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  1120. pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
  1121. bridge->hpp.t0->latency_timer);
  1122. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
  1123. if (bridge->hpp.t0->enable_serr)
  1124. pci_bctl |= PCI_BRIDGE_CTL_SERR;
  1125. else
  1126. pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
  1127. if (bridge->hpp.t0->enable_perr)
  1128. pci_bctl |= PCI_BRIDGE_CTL_PARITY;
  1129. else
  1130. pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
  1131. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
  1132. if (dev->subordinate) {
  1133. list_for_each_entry(cdev, &dev->subordinate->devices,
  1134. bus_list)
  1135. program_hpp(cdev, bridge);
  1136. }
  1137. }
  1138. }
  1139. static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
  1140. {
  1141. struct acpiphp_bridge bridge;
  1142. struct pci_dev *dev;
  1143. memset(&bridge, 0, sizeof(bridge));
  1144. bridge.handle = handle;
  1145. bridge.pci_bus = bus;
  1146. bridge.pci_dev = bus->self;
  1147. decode_hpp(&bridge);
  1148. list_for_each_entry(dev, &bus->devices, bus_list)
  1149. program_hpp(dev, &bridge);
  1150. }
  1151. /*
  1152. * Remove devices for which we could not assign resources, call
  1153. * arch specific code to fix-up the bus
  1154. */
  1155. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  1156. {
  1157. struct pci_dev *dev;
  1158. int i;
  1159. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  1160. list_for_each_entry(dev, &bus->devices, bus_list) {
  1161. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  1162. struct resource *res = &dev->resource[i];
  1163. if ((res->flags & type_mask) && !res->start &&
  1164. res->end) {
  1165. /* Could not assign a required resources
  1166. * for this device, remove it */
  1167. pci_remove_bus_device(dev);
  1168. break;
  1169. }
  1170. }
  1171. }
  1172. }
  1173. /* Program resources in newly inserted bridge */
  1174. static int acpiphp_configure_bridge (acpi_handle handle)
  1175. {
  1176. struct acpi_pci_id pci_id;
  1177. struct pci_bus *bus;
  1178. if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
  1179. err("cannot get PCI domain and bus number for bridge\n");
  1180. return -EINVAL;
  1181. }
  1182. bus = pci_find_bus(pci_id.segment, pci_id.bus);
  1183. if (!bus) {
  1184. err("cannot find bus %d:%d\n",
  1185. pci_id.segment, pci_id.bus);
  1186. return -EINVAL;
  1187. }
  1188. pci_bus_size_bridges(bus);
  1189. pci_bus_assign_resources(bus);
  1190. acpiphp_sanitize_bus(bus);
  1191. acpiphp_set_hpp_values(handle, bus);
  1192. pci_enable_bridges(bus);
  1193. acpiphp_configure_ioapics(handle);
  1194. return 0;
  1195. }
  1196. static void handle_bridge_insertion(acpi_handle handle, u32 type)
  1197. {
  1198. struct acpi_device *device, *pdevice;
  1199. acpi_handle phandle;
  1200. if ((type != ACPI_NOTIFY_BUS_CHECK) &&
  1201. (type != ACPI_NOTIFY_DEVICE_CHECK)) {
  1202. err("unexpected notification type %d\n", type);
  1203. return;
  1204. }
  1205. acpi_get_parent(handle, &phandle);
  1206. if (acpi_bus_get_device(phandle, &pdevice)) {
  1207. dbg("no parent device, assuming NULL\n");
  1208. pdevice = NULL;
  1209. }
  1210. if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
  1211. err("cannot add bridge to acpi list\n");
  1212. return;
  1213. }
  1214. if (!acpiphp_configure_bridge(handle) &&
  1215. !acpi_bus_start(device))
  1216. add_bridge(handle);
  1217. else
  1218. err("cannot configure and start bridge\n");
  1219. }
  1220. /*
  1221. * ACPI event handlers
  1222. */
  1223. static acpi_status
  1224. count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  1225. {
  1226. int *count = (int *)context;
  1227. struct acpiphp_bridge *bridge;
  1228. bridge = acpiphp_handle_to_bridge(handle);
  1229. if (bridge)
  1230. (*count)++;
  1231. return AE_OK ;
  1232. }
  1233. static acpi_status
  1234. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  1235. {
  1236. struct acpiphp_bridge *bridge;
  1237. char objname[64];
  1238. struct acpi_buffer buffer = { .length = sizeof(objname),
  1239. .pointer = objname };
  1240. bridge = acpiphp_handle_to_bridge(handle);
  1241. if (bridge) {
  1242. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1243. dbg("%s: re-enumerating slots under %s\n",
  1244. __func__, objname);
  1245. acpiphp_check_bridge(bridge);
  1246. }
  1247. return AE_OK ;
  1248. }
  1249. /**
  1250. * handle_hotplug_event_bridge - handle ACPI event on bridges
  1251. * @handle: Notify()'ed acpi_handle
  1252. * @type: Notify code
  1253. * @context: pointer to acpiphp_bridge structure
  1254. *
  1255. * Handles ACPI event notification on {host,p2p} bridges.
  1256. */
  1257. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
  1258. {
  1259. struct acpiphp_bridge *bridge;
  1260. char objname[64];
  1261. struct acpi_buffer buffer = { .length = sizeof(objname),
  1262. .pointer = objname };
  1263. struct acpi_device *device;
  1264. int num_sub_bridges = 0;
  1265. if (acpi_bus_get_device(handle, &device)) {
  1266. /* This bridge must have just been physically inserted */
  1267. handle_bridge_insertion(handle, type);
  1268. return;
  1269. }
  1270. bridge = acpiphp_handle_to_bridge(handle);
  1271. if (type == ACPI_NOTIFY_BUS_CHECK) {
  1272. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
  1273. count_sub_bridges, &num_sub_bridges, NULL);
  1274. }
  1275. if (!bridge && !num_sub_bridges) {
  1276. err("cannot get bridge info\n");
  1277. return;
  1278. }
  1279. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1280. switch (type) {
  1281. case ACPI_NOTIFY_BUS_CHECK:
  1282. /* bus re-enumerate */
  1283. dbg("%s: Bus check notify on %s\n", __func__, objname);
  1284. if (bridge) {
  1285. dbg("%s: re-enumerating slots under %s\n",
  1286. __func__, objname);
  1287. acpiphp_check_bridge(bridge);
  1288. }
  1289. if (num_sub_bridges)
  1290. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  1291. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
  1292. break;
  1293. case ACPI_NOTIFY_DEVICE_CHECK:
  1294. /* device check */
  1295. dbg("%s: Device check notify on %s\n", __func__, objname);
  1296. acpiphp_check_bridge(bridge);
  1297. break;
  1298. case ACPI_NOTIFY_DEVICE_WAKE:
  1299. /* wake event */
  1300. dbg("%s: Device wake notify on %s\n", __func__, objname);
  1301. break;
  1302. case ACPI_NOTIFY_EJECT_REQUEST:
  1303. /* request device eject */
  1304. dbg("%s: Device eject notify on %s\n", __func__, objname);
  1305. if ((bridge->type != BRIDGE_TYPE_HOST) &&
  1306. (bridge->flags & BRIDGE_HAS_EJ0)) {
  1307. struct acpiphp_slot *slot;
  1308. slot = bridge->func->slot;
  1309. if (!acpiphp_disable_slot(slot))
  1310. acpiphp_eject_slot(slot);
  1311. }
  1312. break;
  1313. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  1314. printk(KERN_ERR "Device %s cannot be configured due"
  1315. " to a frequency mismatch\n", objname);
  1316. break;
  1317. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  1318. printk(KERN_ERR "Device %s cannot be configured due"
  1319. " to a bus mode mismatch\n", objname);
  1320. break;
  1321. case ACPI_NOTIFY_POWER_FAULT:
  1322. printk(KERN_ERR "Device %s has suffered a power fault\n",
  1323. objname);
  1324. break;
  1325. default:
  1326. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1327. break;
  1328. }
  1329. }
  1330. /**
  1331. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  1332. * @handle: Notify()'ed acpi_handle
  1333. * @type: Notify code
  1334. * @context: pointer to acpiphp_func structure
  1335. *
  1336. * Handles ACPI event notification on slots.
  1337. */
  1338. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
  1339. {
  1340. struct acpiphp_func *func;
  1341. char objname[64];
  1342. struct acpi_buffer buffer = { .length = sizeof(objname),
  1343. .pointer = objname };
  1344. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1345. func = (struct acpiphp_func *)context;
  1346. switch (type) {
  1347. case ACPI_NOTIFY_BUS_CHECK:
  1348. /* bus re-enumerate */
  1349. dbg("%s: Bus check notify on %s\n", __func__, objname);
  1350. acpiphp_enable_slot(func->slot);
  1351. break;
  1352. case ACPI_NOTIFY_DEVICE_CHECK:
  1353. /* device check : re-enumerate from parent bus */
  1354. dbg("%s: Device check notify on %s\n", __func__, objname);
  1355. acpiphp_check_bridge(func->slot->bridge);
  1356. break;
  1357. case ACPI_NOTIFY_DEVICE_WAKE:
  1358. /* wake event */
  1359. dbg("%s: Device wake notify on %s\n", __func__, objname);
  1360. break;
  1361. case ACPI_NOTIFY_EJECT_REQUEST:
  1362. /* request device eject */
  1363. dbg("%s: Device eject notify on %s\n", __func__, objname);
  1364. if (!(acpiphp_disable_slot(func->slot)))
  1365. acpiphp_eject_slot(func->slot);
  1366. break;
  1367. default:
  1368. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1369. break;
  1370. }
  1371. }
  1372. static acpi_status
  1373. find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  1374. {
  1375. int *count = (int *)context;
  1376. if (acpi_root_bridge(handle)) {
  1377. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  1378. handle_hotplug_event_bridge, NULL);
  1379. (*count)++;
  1380. }
  1381. return AE_OK ;
  1382. }
  1383. static struct acpi_pci_driver acpi_pci_hp_driver = {
  1384. .add = add_bridge,
  1385. .remove = remove_bridge,
  1386. };
  1387. /**
  1388. * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
  1389. */
  1390. int __init acpiphp_glue_init(void)
  1391. {
  1392. int num = 0;
  1393. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  1394. ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
  1395. if (num <= 0)
  1396. return -1;
  1397. else
  1398. acpi_pci_register_driver(&acpi_pci_hp_driver);
  1399. return 0;
  1400. }
  1401. /**
  1402. * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
  1403. *
  1404. * This function frees all data allocated in acpiphp_glue_init().
  1405. */
  1406. void acpiphp_glue_exit(void)
  1407. {
  1408. acpi_pci_unregister_driver(&acpi_pci_hp_driver);
  1409. }
  1410. /**
  1411. * acpiphp_get_num_slots - count number of slots in a system
  1412. */
  1413. int __init acpiphp_get_num_slots(void)
  1414. {
  1415. struct acpiphp_bridge *bridge;
  1416. int num_slots = 0;
  1417. list_for_each_entry (bridge, &bridge_list, list) {
  1418. dbg("Bus %04x:%02x has %d slot%s\n",
  1419. pci_domain_nr(bridge->pci_bus),
  1420. bridge->pci_bus->number, bridge->nr_slots,
  1421. bridge->nr_slots == 1 ? "" : "s");
  1422. num_slots += bridge->nr_slots;
  1423. }
  1424. dbg("Total %d slots\n", num_slots);
  1425. return num_slots;
  1426. }
  1427. #if 0
  1428. /**
  1429. * acpiphp_for_each_slot - call function for each slot
  1430. * @fn: callback function
  1431. * @data: context to be passed to callback function
  1432. */
  1433. static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
  1434. {
  1435. struct list_head *node;
  1436. struct acpiphp_bridge *bridge;
  1437. struct acpiphp_slot *slot;
  1438. int retval = 0;
  1439. list_for_each (node, &bridge_list) {
  1440. bridge = (struct acpiphp_bridge *)node;
  1441. for (slot = bridge->slots; slot; slot = slot->next) {
  1442. retval = fn(slot, data);
  1443. if (!retval)
  1444. goto err_exit;
  1445. }
  1446. }
  1447. err_exit:
  1448. return retval;
  1449. }
  1450. #endif
  1451. /**
  1452. * acpiphp_enable_slot - power on slot
  1453. * @slot: ACPI PHP slot
  1454. */
  1455. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1456. {
  1457. int retval;
  1458. mutex_lock(&slot->crit_sect);
  1459. /* wake up all functions */
  1460. retval = power_on_slot(slot);
  1461. if (retval)
  1462. goto err_exit;
  1463. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1464. /* configure all functions */
  1465. retval = enable_device(slot);
  1466. if (retval)
  1467. power_off_slot(slot);
  1468. } else {
  1469. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  1470. power_off_slot(slot);
  1471. }
  1472. err_exit:
  1473. mutex_unlock(&slot->crit_sect);
  1474. return retval;
  1475. }
  1476. /**
  1477. * acpiphp_disable_slot - power off slot
  1478. * @slot: ACPI PHP slot
  1479. */
  1480. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1481. {
  1482. int retval = 0;
  1483. mutex_lock(&slot->crit_sect);
  1484. /* unconfigure all functions */
  1485. retval = disable_device(slot);
  1486. if (retval)
  1487. goto err_exit;
  1488. /* power off all functions */
  1489. retval = power_off_slot(slot);
  1490. if (retval)
  1491. goto err_exit;
  1492. err_exit:
  1493. mutex_unlock(&slot->crit_sect);
  1494. return retval;
  1495. }
  1496. /*
  1497. * slot enabled: 1
  1498. * slot disabled: 0
  1499. */
  1500. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1501. {
  1502. return (slot->flags & SLOT_POWEREDON);
  1503. }
  1504. /*
  1505. * latch open: 1
  1506. * latch closed: 0
  1507. */
  1508. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1509. {
  1510. unsigned int sta;
  1511. sta = get_slot_status(slot);
  1512. return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
  1513. }
  1514. /*
  1515. * adapter presence : 1
  1516. * absence : 0
  1517. */
  1518. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1519. {
  1520. unsigned int sta;
  1521. sta = get_slot_status(slot);
  1522. return (sta == 0) ? 0 : 1;
  1523. }