acpiphp_glue.c 43 KB

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