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