acpiphp_glue.c 37 KB

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