acpiphp_glue.c 40 KB

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