acpiphp_glue.c 44 KB

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