acpiphp_glue.c 43 KB

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