acpiphp_glue.c 43 KB

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