acpiphp_glue.c 34 KB

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