acpiphp_glue.c 43 KB

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