acpiphp_glue.c 36 KB

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