acpiphp_glue.c 41 KB

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