acpiphp_glue.c 42 KB

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