acpiphp_glue.c 39 KB

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