acpiphp_glue.c 43 KB

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