acpiphp_glue.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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. *
  8. * All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  18. * NON INFRINGEMENT. See the GNU General Public License for more
  19. * details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. * Send feedback to <t-kochi@bq.jp.nec.com>
  26. *
  27. */
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/pci.h>
  32. #include <linux/smp_lock.h>
  33. #include <asm/semaphore.h>
  34. #include "../pci.h"
  35. #include "pci_hotplug.h"
  36. #include "acpiphp.h"
  37. static LIST_HEAD(bridge_list);
  38. #define MY_NAME "acpiphp_glue"
  39. static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
  40. static void handle_hotplug_event_func (acpi_handle, u32, void *);
  41. /*
  42. * initialization & terminatation routines
  43. */
  44. /**
  45. * is_ejectable - determine if a slot is ejectable
  46. * @handle: handle to acpi namespace
  47. *
  48. * Ejectable slot should satisfy at least these conditions:
  49. *
  50. * 1. has _ADR method
  51. * 2. has _EJ0 method
  52. *
  53. * optionally
  54. *
  55. * 1. has _STA method
  56. * 2. has _PS0 method
  57. * 3. has _PS3 method
  58. * 4. ..
  59. *
  60. */
  61. static int is_ejectable(acpi_handle handle)
  62. {
  63. acpi_status status;
  64. acpi_handle tmp;
  65. status = acpi_get_handle(handle, "_ADR", &tmp);
  66. if (ACPI_FAILURE(status)) {
  67. return 0;
  68. }
  69. status = acpi_get_handle(handle, "_EJ0", &tmp);
  70. if (ACPI_FAILURE(status)) {
  71. return 0;
  72. }
  73. return 1;
  74. }
  75. /* callback routine to check the existence of ejectable slots */
  76. static acpi_status
  77. is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  78. {
  79. int *count = (int *)context;
  80. if (is_ejectable(handle)) {
  81. (*count)++;
  82. /* only one ejectable slot is enough */
  83. return AE_CTRL_TERMINATE;
  84. } else {
  85. return AE_OK;
  86. }
  87. }
  88. /* callback routine to register each ACPI PCI slot object */
  89. static acpi_status
  90. register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  91. {
  92. struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
  93. struct acpiphp_slot *slot;
  94. struct acpiphp_func *newfunc;
  95. acpi_handle tmp;
  96. acpi_status status = AE_OK;
  97. unsigned long adr, sun;
  98. int device, function;
  99. static int num_slots = 0; /* XXX if we support I/O node hotplug... */
  100. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  101. if (ACPI_FAILURE(status))
  102. return AE_OK;
  103. status = acpi_get_handle(handle, "_EJ0", &tmp);
  104. if (ACPI_FAILURE(status))
  105. return AE_OK;
  106. device = (adr >> 16) & 0xffff;
  107. function = adr & 0xffff;
  108. newfunc = kmalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
  109. if (!newfunc)
  110. return AE_NO_MEMORY;
  111. memset(newfunc, 0, sizeof(struct acpiphp_func));
  112. INIT_LIST_HEAD(&newfunc->sibling);
  113. newfunc->handle = handle;
  114. newfunc->function = function;
  115. newfunc->flags = FUNC_HAS_EJ0;
  116. if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
  117. newfunc->flags |= FUNC_HAS_STA;
  118. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
  119. newfunc->flags |= FUNC_HAS_PS0;
  120. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
  121. newfunc->flags |= FUNC_HAS_PS3;
  122. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  123. if (ACPI_FAILURE(status))
  124. sun = -1;
  125. /* search for objects that share the same slot */
  126. for (slot = bridge->slots; slot; slot = slot->next)
  127. if (slot->device == device) {
  128. if (slot->sun != sun)
  129. warn("sibling found, but _SUN doesn't match!\n");
  130. break;
  131. }
  132. if (!slot) {
  133. slot = kmalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  134. if (!slot) {
  135. kfree(newfunc);
  136. return AE_NO_MEMORY;
  137. }
  138. memset(slot, 0, sizeof(struct acpiphp_slot));
  139. slot->bridge = bridge;
  140. slot->id = num_slots++;
  141. slot->device = device;
  142. slot->sun = sun;
  143. INIT_LIST_HEAD(&slot->funcs);
  144. init_MUTEX(&slot->crit_sect);
  145. slot->next = bridge->slots;
  146. bridge->slots = slot;
  147. bridge->nr_slots++;
  148. dbg("found ACPI PCI Hotplug slot at PCI %02x:%02x Slot:%d\n",
  149. slot->bridge->bus, slot->device, slot->sun);
  150. }
  151. newfunc->slot = slot;
  152. list_add_tail(&newfunc->sibling, &slot->funcs);
  153. /* associate corresponding pci_dev */
  154. newfunc->pci_dev = pci_find_slot(bridge->bus,
  155. PCI_DEVFN(device, function));
  156. if (newfunc->pci_dev) {
  157. if (acpiphp_init_func_resource(newfunc) < 0) {
  158. kfree(newfunc);
  159. return AE_ERROR;
  160. }
  161. slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
  162. }
  163. /* install notify handler */
  164. status = acpi_install_notify_handler(handle,
  165. ACPI_SYSTEM_NOTIFY,
  166. handle_hotplug_event_func,
  167. newfunc);
  168. if (ACPI_FAILURE(status)) {
  169. err("failed to register interrupt notify handler\n");
  170. return status;
  171. }
  172. return AE_OK;
  173. }
  174. /* see if it's worth looking at this bridge */
  175. static int detect_ejectable_slots(acpi_handle *bridge_handle)
  176. {
  177. acpi_status status;
  178. int count;
  179. count = 0;
  180. /* only check slots defined directly below bridge object */
  181. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
  182. is_ejectable_slot, (void *)&count, NULL);
  183. return count;
  184. }
  185. /* decode ACPI _CRS data and convert into our internal resource list
  186. * TBD: _TRA, etc.
  187. */
  188. static acpi_status
  189. decode_acpi_resource(struct acpi_resource *resource, void *context)
  190. {
  191. struct acpiphp_bridge *bridge = (struct acpiphp_bridge *) context;
  192. struct acpi_resource_address64 address;
  193. struct pci_resource *res;
  194. if (resource->id != ACPI_RSTYPE_ADDRESS16 &&
  195. resource->id != ACPI_RSTYPE_ADDRESS32 &&
  196. resource->id != ACPI_RSTYPE_ADDRESS64)
  197. return AE_OK;
  198. acpi_resource_to_address64(resource, &address);
  199. if (address.producer_consumer == ACPI_PRODUCER && address.address_length > 0) {
  200. dbg("resource type: %d: 0x%llx - 0x%llx\n", address.resource_type,
  201. (unsigned long long)address.min_address_range,
  202. (unsigned long long)address.max_address_range);
  203. res = acpiphp_make_resource(address.min_address_range,
  204. address.address_length);
  205. if (!res) {
  206. err("out of memory\n");
  207. return AE_OK;
  208. }
  209. switch (address.resource_type) {
  210. case ACPI_MEMORY_RANGE:
  211. if (address.attribute.memory.cache_attribute == ACPI_PREFETCHABLE_MEMORY) {
  212. res->next = bridge->p_mem_head;
  213. bridge->p_mem_head = res;
  214. } else {
  215. res->next = bridge->mem_head;
  216. bridge->mem_head = res;
  217. }
  218. break;
  219. case ACPI_IO_RANGE:
  220. res->next = bridge->io_head;
  221. bridge->io_head = res;
  222. break;
  223. case ACPI_BUS_NUMBER_RANGE:
  224. res->next = bridge->bus_head;
  225. bridge->bus_head = res;
  226. break;
  227. default:
  228. /* invalid type */
  229. kfree(res);
  230. break;
  231. }
  232. }
  233. return AE_OK;
  234. }
  235. /* decode ACPI 2.0 _HPP hot plug parameters */
  236. static void decode_hpp(struct acpiphp_bridge *bridge)
  237. {
  238. acpi_status status;
  239. struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
  240. .pointer = NULL};
  241. union acpi_object *package;
  242. int i;
  243. /* default numbers */
  244. bridge->hpp.cache_line_size = 0x10;
  245. bridge->hpp.latency_timer = 0x40;
  246. bridge->hpp.enable_SERR = 0;
  247. bridge->hpp.enable_PERR = 0;
  248. status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer);
  249. if (ACPI_FAILURE(status)) {
  250. dbg("_HPP evaluation failed\n");
  251. return;
  252. }
  253. package = (union acpi_object *) buffer.pointer;
  254. if (!package || package->type != ACPI_TYPE_PACKAGE ||
  255. package->package.count != 4 || !package->package.elements) {
  256. err("invalid _HPP object; ignoring\n");
  257. goto err_exit;
  258. }
  259. for (i = 0; i < 4; i++) {
  260. if (package->package.elements[i].type != ACPI_TYPE_INTEGER) {
  261. err("invalid _HPP parameter type; ignoring\n");
  262. goto err_exit;
  263. }
  264. }
  265. bridge->hpp.cache_line_size = package->package.elements[0].integer.value;
  266. bridge->hpp.latency_timer = package->package.elements[1].integer.value;
  267. bridge->hpp.enable_SERR = package->package.elements[2].integer.value;
  268. bridge->hpp.enable_PERR = package->package.elements[3].integer.value;
  269. dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
  270. bridge->hpp.cache_line_size,
  271. bridge->hpp.latency_timer,
  272. bridge->hpp.enable_SERR,
  273. bridge->hpp.enable_PERR);
  274. bridge->flags |= BRIDGE_HAS_HPP;
  275. err_exit:
  276. kfree(buffer.pointer);
  277. }
  278. /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
  279. static void init_bridge_misc(struct acpiphp_bridge *bridge)
  280. {
  281. acpi_status status;
  282. /* decode ACPI 2.0 _HPP (hot plug parameters) */
  283. decode_hpp(bridge);
  284. /* subtract all resources already allocated */
  285. acpiphp_detect_pci_resource(bridge);
  286. /* register all slot objects under this bridge */
  287. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
  288. register_slot, bridge, NULL);
  289. /* install notify handler */
  290. status = acpi_install_notify_handler(bridge->handle,
  291. ACPI_SYSTEM_NOTIFY,
  292. handle_hotplug_event_bridge,
  293. bridge);
  294. if (ACPI_FAILURE(status)) {
  295. err("failed to register interrupt notify handler\n");
  296. }
  297. list_add(&bridge->list, &bridge_list);
  298. dbg("Bridge resource:\n");
  299. acpiphp_dump_resource(bridge);
  300. }
  301. /* allocate and initialize host bridge data structure */
  302. static void add_host_bridge(acpi_handle *handle, int seg, int bus)
  303. {
  304. acpi_status status;
  305. struct acpiphp_bridge *bridge;
  306. bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  307. if (bridge == NULL)
  308. return;
  309. memset(bridge, 0, sizeof(struct acpiphp_bridge));
  310. bridge->type = BRIDGE_TYPE_HOST;
  311. bridge->handle = handle;
  312. bridge->seg = seg;
  313. bridge->bus = bus;
  314. bridge->pci_bus = pci_find_bus(seg, bus);
  315. spin_lock_init(&bridge->res_lock);
  316. /* to be overridden when we decode _CRS */
  317. bridge->sub = bridge->bus;
  318. /* decode resources */
  319. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  320. decode_acpi_resource, bridge);
  321. if (ACPI_FAILURE(status)) {
  322. err("failed to decode bridge resources\n");
  323. kfree(bridge);
  324. return;
  325. }
  326. acpiphp_resource_sort_and_combine(&bridge->io_head);
  327. acpiphp_resource_sort_and_combine(&bridge->mem_head);
  328. acpiphp_resource_sort_and_combine(&bridge->p_mem_head);
  329. acpiphp_resource_sort_and_combine(&bridge->bus_head);
  330. dbg("ACPI _CRS resource:\n");
  331. acpiphp_dump_resource(bridge);
  332. if (bridge->bus_head) {
  333. bridge->bus = bridge->bus_head->base;
  334. bridge->sub = bridge->bus_head->base + bridge->bus_head->length - 1;
  335. }
  336. init_bridge_misc(bridge);
  337. }
  338. /* allocate and initialize PCI-to-PCI bridge data structure */
  339. static void add_p2p_bridge(acpi_handle *handle, int seg, int bus, int dev, int fn)
  340. {
  341. struct acpiphp_bridge *bridge;
  342. u8 tmp8;
  343. u16 tmp16;
  344. u64 base64, limit64;
  345. u32 base, limit, base32u, limit32u;
  346. bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  347. if (bridge == NULL) {
  348. err("out of memory\n");
  349. return;
  350. }
  351. memset(bridge, 0, sizeof(struct acpiphp_bridge));
  352. bridge->type = BRIDGE_TYPE_P2P;
  353. bridge->handle = handle;
  354. bridge->seg = seg;
  355. bridge->pci_dev = pci_find_slot(bus, PCI_DEVFN(dev, fn));
  356. if (!bridge->pci_dev) {
  357. err("Can't get pci_dev\n");
  358. kfree(bridge);
  359. return;
  360. }
  361. bridge->pci_bus = bridge->pci_dev->subordinate;
  362. if (!bridge->pci_bus) {
  363. err("This is not a PCI-to-PCI bridge!\n");
  364. kfree(bridge);
  365. return;
  366. }
  367. spin_lock_init(&bridge->res_lock);
  368. bridge->bus = bridge->pci_bus->number;
  369. bridge->sub = bridge->pci_bus->subordinate;
  370. /*
  371. * decode resources under this P2P bridge
  372. */
  373. /* I/O resources */
  374. pci_read_config_byte(bridge->pci_dev, PCI_IO_BASE, &tmp8);
  375. base = tmp8;
  376. pci_read_config_byte(bridge->pci_dev, PCI_IO_LIMIT, &tmp8);
  377. limit = tmp8;
  378. switch (base & PCI_IO_RANGE_TYPE_MASK) {
  379. case PCI_IO_RANGE_TYPE_16:
  380. base = (base << 8) & 0xf000;
  381. limit = ((limit << 8) & 0xf000) + 0xfff;
  382. bridge->io_head = acpiphp_make_resource((u64)base, limit - base + 1);
  383. if (!bridge->io_head) {
  384. err("out of memory\n");
  385. kfree(bridge);
  386. return;
  387. }
  388. dbg("16bit I/O range: %04x-%04x\n",
  389. (u32)bridge->io_head->base,
  390. (u32)(bridge->io_head->base + bridge->io_head->length - 1));
  391. break;
  392. case PCI_IO_RANGE_TYPE_32:
  393. pci_read_config_word(bridge->pci_dev, PCI_IO_BASE_UPPER16, &tmp16);
  394. base = ((u32)tmp16 << 16) | ((base << 8) & 0xf000);
  395. pci_read_config_word(bridge->pci_dev, PCI_IO_LIMIT_UPPER16, &tmp16);
  396. limit = (((u32)tmp16 << 16) | ((limit << 8) & 0xf000)) + 0xfff;
  397. bridge->io_head = acpiphp_make_resource((u64)base, limit - base + 1);
  398. if (!bridge->io_head) {
  399. err("out of memory\n");
  400. kfree(bridge);
  401. return;
  402. }
  403. dbg("32bit I/O range: %08x-%08x\n",
  404. (u32)bridge->io_head->base,
  405. (u32)(bridge->io_head->base + bridge->io_head->length - 1));
  406. break;
  407. case 0x0f:
  408. dbg("I/O space unsupported\n");
  409. break;
  410. default:
  411. warn("Unknown I/O range type\n");
  412. }
  413. /* Memory resources (mandatory for P2P bridge) */
  414. pci_read_config_word(bridge->pci_dev, PCI_MEMORY_BASE, &tmp16);
  415. base = (tmp16 & 0xfff0) << 16;
  416. pci_read_config_word(bridge->pci_dev, PCI_MEMORY_LIMIT, &tmp16);
  417. limit = ((tmp16 & 0xfff0) << 16) | 0xfffff;
  418. bridge->mem_head = acpiphp_make_resource((u64)base, limit - base + 1);
  419. if (!bridge->mem_head) {
  420. err("out of memory\n");
  421. kfree(bridge);
  422. return;
  423. }
  424. dbg("32bit Memory range: %08x-%08x\n",
  425. (u32)bridge->mem_head->base,
  426. (u32)(bridge->mem_head->base + bridge->mem_head->length-1));
  427. /* Prefetchable Memory resources (optional) */
  428. pci_read_config_word(bridge->pci_dev, PCI_PREF_MEMORY_BASE, &tmp16);
  429. base = tmp16;
  430. pci_read_config_word(bridge->pci_dev, PCI_PREF_MEMORY_LIMIT, &tmp16);
  431. limit = tmp16;
  432. switch (base & PCI_MEMORY_RANGE_TYPE_MASK) {
  433. case PCI_PREF_RANGE_TYPE_32:
  434. base = (base & 0xfff0) << 16;
  435. limit = ((limit & 0xfff0) << 16) | 0xfffff;
  436. bridge->p_mem_head = acpiphp_make_resource((u64)base, limit - base + 1);
  437. if (!bridge->p_mem_head) {
  438. err("out of memory\n");
  439. kfree(bridge);
  440. return;
  441. }
  442. dbg("32bit Prefetchable memory range: %08x-%08x\n",
  443. (u32)bridge->p_mem_head->base,
  444. (u32)(bridge->p_mem_head->base + bridge->p_mem_head->length - 1));
  445. break;
  446. case PCI_PREF_RANGE_TYPE_64:
  447. pci_read_config_dword(bridge->pci_dev, PCI_PREF_BASE_UPPER32, &base32u);
  448. pci_read_config_dword(bridge->pci_dev, PCI_PREF_LIMIT_UPPER32, &limit32u);
  449. base64 = ((u64)base32u << 32) | ((base & 0xfff0) << 16);
  450. limit64 = (((u64)limit32u << 32) | ((limit & 0xfff0) << 16)) + 0xfffff;
  451. bridge->p_mem_head = acpiphp_make_resource(base64, limit64 - base64 + 1);
  452. if (!bridge->p_mem_head) {
  453. err("out of memory\n");
  454. kfree(bridge);
  455. return;
  456. }
  457. dbg("64bit Prefetchable memory range: %08x%08x-%08x%08x\n",
  458. (u32)(bridge->p_mem_head->base >> 32),
  459. (u32)(bridge->p_mem_head->base & 0xffffffff),
  460. (u32)((bridge->p_mem_head->base + bridge->p_mem_head->length - 1) >> 32),
  461. (u32)((bridge->p_mem_head->base + bridge->p_mem_head->length - 1) & 0xffffffff));
  462. break;
  463. case 0x0f:
  464. break;
  465. default:
  466. warn("Unknown prefetchale memory type\n");
  467. }
  468. init_bridge_misc(bridge);
  469. }
  470. /* callback routine to find P2P bridges */
  471. static acpi_status
  472. find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  473. {
  474. acpi_status status;
  475. acpi_handle dummy_handle;
  476. unsigned long *segbus = context;
  477. unsigned long tmp;
  478. int seg, bus, device, function;
  479. struct pci_dev *dev;
  480. /* get PCI address */
  481. seg = (*segbus >> 8) & 0xff;
  482. bus = *segbus & 0xff;
  483. status = acpi_get_handle(handle, "_ADR", &dummy_handle);
  484. if (ACPI_FAILURE(status))
  485. return AE_OK; /* continue */
  486. status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
  487. if (ACPI_FAILURE(status)) {
  488. dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
  489. return AE_OK;
  490. }
  491. device = (tmp >> 16) & 0xffff;
  492. function = tmp & 0xffff;
  493. dev = pci_find_slot(bus, PCI_DEVFN(device, function));
  494. if (!dev)
  495. return AE_OK;
  496. if (!dev->subordinate)
  497. return AE_OK;
  498. /* check if this bridge has ejectable slots */
  499. if (detect_ejectable_slots(handle) > 0) {
  500. dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
  501. add_p2p_bridge(handle, seg, bus, device, function);
  502. }
  503. return AE_OK;
  504. }
  505. /* find hot-pluggable slots, and then find P2P bridge */
  506. static int add_bridge(acpi_handle handle)
  507. {
  508. acpi_status status;
  509. unsigned long tmp;
  510. int seg, bus;
  511. acpi_handle dummy_handle;
  512. /* if the bridge doesn't have _STA, we assume it is always there */
  513. status = acpi_get_handle(handle, "_STA", &dummy_handle);
  514. if (ACPI_SUCCESS(status)) {
  515. status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
  516. if (ACPI_FAILURE(status)) {
  517. dbg("%s: _STA evaluation failure\n", __FUNCTION__);
  518. return 0;
  519. }
  520. if ((tmp & ACPI_STA_FUNCTIONING) == 0)
  521. /* don't register this object */
  522. return 0;
  523. }
  524. /* get PCI segment number */
  525. status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
  526. seg = ACPI_SUCCESS(status) ? tmp : 0;
  527. /* get PCI bus number */
  528. status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
  529. if (ACPI_SUCCESS(status)) {
  530. bus = tmp;
  531. } else {
  532. warn("can't get bus number, assuming 0\n");
  533. bus = 0;
  534. }
  535. /* check if this bridge has ejectable slots */
  536. if (detect_ejectable_slots(handle) > 0) {
  537. dbg("found PCI host-bus bridge with hot-pluggable slots\n");
  538. add_host_bridge(handle, seg, bus);
  539. return 0;
  540. }
  541. tmp = seg << 8 | bus;
  542. /* search P2P bridges under this host bridge */
  543. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  544. find_p2p_bridge, &tmp, NULL);
  545. if (ACPI_FAILURE(status))
  546. warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
  547. return 0;
  548. }
  549. static void remove_bridge(acpi_handle handle)
  550. {
  551. /* No-op for now .. */
  552. }
  553. static int power_on_slot(struct acpiphp_slot *slot)
  554. {
  555. acpi_status status;
  556. struct acpiphp_func *func;
  557. struct list_head *l;
  558. int retval = 0;
  559. /* if already enabled, just skip */
  560. if (slot->flags & SLOT_POWEREDON)
  561. goto err_exit;
  562. list_for_each (l, &slot->funcs) {
  563. func = list_entry(l, struct acpiphp_func, sibling);
  564. if (func->flags & FUNC_HAS_PS0) {
  565. dbg("%s: executing _PS0\n", __FUNCTION__);
  566. status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
  567. if (ACPI_FAILURE(status)) {
  568. warn("%s: _PS0 failed\n", __FUNCTION__);
  569. retval = -1;
  570. goto err_exit;
  571. } else
  572. break;
  573. }
  574. }
  575. /* TBD: evaluate _STA to check if the slot is enabled */
  576. slot->flags |= SLOT_POWEREDON;
  577. err_exit:
  578. return retval;
  579. }
  580. static int power_off_slot(struct acpiphp_slot *slot)
  581. {
  582. acpi_status status;
  583. struct acpiphp_func *func;
  584. struct list_head *l;
  585. struct acpi_object_list arg_list;
  586. union acpi_object arg;
  587. int retval = 0;
  588. /* if already disabled, just skip */
  589. if ((slot->flags & SLOT_POWEREDON) == 0)
  590. goto err_exit;
  591. list_for_each (l, &slot->funcs) {
  592. func = list_entry(l, struct acpiphp_func, sibling);
  593. if (func->pci_dev && (func->flags & FUNC_HAS_PS3)) {
  594. status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
  595. if (ACPI_FAILURE(status)) {
  596. warn("%s: _PS3 failed\n", __FUNCTION__);
  597. retval = -1;
  598. goto err_exit;
  599. } else
  600. break;
  601. }
  602. }
  603. list_for_each (l, &slot->funcs) {
  604. func = list_entry(l, struct acpiphp_func, sibling);
  605. /* We don't want to call _EJ0 on non-existing functions. */
  606. if (func->pci_dev && (func->flags & FUNC_HAS_EJ0)) {
  607. /* _EJ0 method take one argument */
  608. arg_list.count = 1;
  609. arg_list.pointer = &arg;
  610. arg.type = ACPI_TYPE_INTEGER;
  611. arg.integer.value = 1;
  612. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  613. if (ACPI_FAILURE(status)) {
  614. warn("%s: _EJ0 failed\n", __FUNCTION__);
  615. retval = -1;
  616. goto err_exit;
  617. } else
  618. break;
  619. }
  620. }
  621. /* TBD: evaluate _STA to check if the slot is disabled */
  622. slot->flags &= (~SLOT_POWEREDON);
  623. err_exit:
  624. return retval;
  625. }
  626. /**
  627. * enable_device - enable, configure a slot
  628. * @slot: slot to be enabled
  629. *
  630. * This function should be called per *physical slot*,
  631. * not per each slot object in ACPI namespace.
  632. *
  633. */
  634. static int enable_device(struct acpiphp_slot *slot)
  635. {
  636. u8 bus;
  637. struct pci_dev *dev;
  638. struct pci_bus *child;
  639. struct list_head *l;
  640. struct acpiphp_func *func;
  641. int retval = 0;
  642. int num;
  643. if (slot->flags & SLOT_ENABLED)
  644. goto err_exit;
  645. /* sanity check: dev should be NULL when hot-plugged in */
  646. dev = pci_find_slot(slot->bridge->bus, PCI_DEVFN(slot->device, 0));
  647. if (dev) {
  648. /* This case shouldn't happen */
  649. err("pci_dev structure already exists.\n");
  650. retval = -1;
  651. goto err_exit;
  652. }
  653. /* allocate resources to device */
  654. retval = acpiphp_configure_slot(slot);
  655. if (retval)
  656. goto err_exit;
  657. /* returned `dev' is the *first function* only! */
  658. num = pci_scan_slot(slot->bridge->pci_bus, PCI_DEVFN(slot->device, 0));
  659. if (num)
  660. pci_bus_add_devices(slot->bridge->pci_bus);
  661. dev = pci_find_slot(slot->bridge->bus, PCI_DEVFN(slot->device, 0));
  662. if (!dev) {
  663. err("No new device found\n");
  664. retval = -1;
  665. goto err_exit;
  666. }
  667. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  668. pci_read_config_byte(dev, PCI_SECONDARY_BUS, &bus);
  669. child = (struct pci_bus*) pci_add_new_bus(dev->bus, dev, bus);
  670. pci_do_scan_bus(child);
  671. }
  672. /* associate pci_dev to our representation */
  673. list_for_each (l, &slot->funcs) {
  674. func = list_entry(l, struct acpiphp_func, sibling);
  675. func->pci_dev = pci_find_slot(slot->bridge->bus,
  676. PCI_DEVFN(slot->device,
  677. func->function));
  678. if (!func->pci_dev)
  679. continue;
  680. /* configure device */
  681. retval = acpiphp_configure_function(func);
  682. if (retval)
  683. goto err_exit;
  684. }
  685. slot->flags |= SLOT_ENABLED;
  686. dbg("Available resources:\n");
  687. acpiphp_dump_resource(slot->bridge);
  688. err_exit:
  689. return retval;
  690. }
  691. /**
  692. * disable_device - disable a slot
  693. */
  694. static int disable_device(struct acpiphp_slot *slot)
  695. {
  696. int retval = 0;
  697. struct acpiphp_func *func;
  698. struct list_head *l;
  699. /* is this slot already disabled? */
  700. if (!(slot->flags & SLOT_ENABLED))
  701. goto err_exit;
  702. list_for_each (l, &slot->funcs) {
  703. func = list_entry(l, struct acpiphp_func, sibling);
  704. if (func->pci_dev)
  705. acpiphp_unconfigure_function(func);
  706. }
  707. slot->flags &= (~SLOT_ENABLED);
  708. err_exit:
  709. return retval;
  710. }
  711. /**
  712. * get_slot_status - get ACPI slot status
  713. *
  714. * if a slot has _STA for each function and if any one of them
  715. * returned non-zero status, return it
  716. *
  717. * if a slot doesn't have _STA and if any one of its functions'
  718. * configuration space is configured, return 0x0f as a _STA
  719. *
  720. * otherwise return 0
  721. */
  722. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  723. {
  724. acpi_status status;
  725. unsigned long sta = 0;
  726. u32 dvid;
  727. struct list_head *l;
  728. struct acpiphp_func *func;
  729. list_for_each (l, &slot->funcs) {
  730. func = list_entry(l, struct acpiphp_func, sibling);
  731. if (func->flags & FUNC_HAS_STA) {
  732. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  733. if (ACPI_SUCCESS(status) && sta)
  734. break;
  735. } else {
  736. pci_bus_read_config_dword(slot->bridge->pci_bus,
  737. PCI_DEVFN(slot->device,
  738. func->function),
  739. PCI_VENDOR_ID, &dvid);
  740. if (dvid != 0xffffffff) {
  741. sta = ACPI_STA_ALL;
  742. break;
  743. }
  744. }
  745. }
  746. return (unsigned int)sta;
  747. }
  748. /**
  749. * acpiphp_check_bridge - re-enumerate devices
  750. *
  751. * Iterate over all slots under this bridge and make sure that if a
  752. * card is present they are enabled, and if not they are disabled.
  753. */
  754. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  755. {
  756. struct acpiphp_slot *slot;
  757. int retval = 0;
  758. int enabled, disabled;
  759. enabled = disabled = 0;
  760. for (slot = bridge->slots; slot; slot = slot->next) {
  761. unsigned int status = get_slot_status(slot);
  762. if (slot->flags & SLOT_ENABLED) {
  763. if (status == ACPI_STA_ALL)
  764. continue;
  765. retval = acpiphp_disable_slot(slot);
  766. if (retval) {
  767. err("Error occurred in disabling\n");
  768. goto err_exit;
  769. }
  770. disabled++;
  771. } else {
  772. if (status != ACPI_STA_ALL)
  773. continue;
  774. retval = acpiphp_enable_slot(slot);
  775. if (retval) {
  776. err("Error occurred in enabling\n");
  777. goto err_exit;
  778. }
  779. enabled++;
  780. }
  781. }
  782. dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
  783. err_exit:
  784. return retval;
  785. }
  786. /*
  787. * ACPI event handlers
  788. */
  789. /**
  790. * handle_hotplug_event_bridge - handle ACPI event on bridges
  791. *
  792. * @handle: Notify()'ed acpi_handle
  793. * @type: Notify code
  794. * @context: pointer to acpiphp_bridge structure
  795. *
  796. * handles ACPI event notification on {host,p2p} bridges
  797. *
  798. */
  799. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
  800. {
  801. struct acpiphp_bridge *bridge;
  802. char objname[64];
  803. struct acpi_buffer buffer = { .length = sizeof(objname),
  804. .pointer = objname };
  805. bridge = (struct acpiphp_bridge *)context;
  806. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  807. switch (type) {
  808. case ACPI_NOTIFY_BUS_CHECK:
  809. /* bus re-enumerate */
  810. dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
  811. acpiphp_check_bridge(bridge);
  812. break;
  813. case ACPI_NOTIFY_DEVICE_CHECK:
  814. /* device check */
  815. dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
  816. acpiphp_check_bridge(bridge);
  817. break;
  818. case ACPI_NOTIFY_DEVICE_WAKE:
  819. /* wake event */
  820. dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
  821. break;
  822. case ACPI_NOTIFY_EJECT_REQUEST:
  823. /* request device eject */
  824. dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
  825. break;
  826. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  827. printk(KERN_ERR "Device %s cannot be configured due"
  828. " to a frequency mismatch\n", objname);
  829. break;
  830. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  831. printk(KERN_ERR "Device %s cannot be configured due"
  832. " to a bus mode mismatch\n", objname);
  833. break;
  834. case ACPI_NOTIFY_POWER_FAULT:
  835. printk(KERN_ERR "Device %s has suffered a power fault\n",
  836. objname);
  837. break;
  838. default:
  839. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  840. break;
  841. }
  842. }
  843. /**
  844. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  845. *
  846. * @handle: Notify()'ed acpi_handle
  847. * @type: Notify code
  848. * @context: pointer to acpiphp_func structure
  849. *
  850. * handles ACPI event notification on slots
  851. *
  852. */
  853. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
  854. {
  855. struct acpiphp_func *func;
  856. char objname[64];
  857. struct acpi_buffer buffer = { .length = sizeof(objname),
  858. .pointer = objname };
  859. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  860. func = (struct acpiphp_func *)context;
  861. switch (type) {
  862. case ACPI_NOTIFY_BUS_CHECK:
  863. /* bus re-enumerate */
  864. dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
  865. acpiphp_enable_slot(func->slot);
  866. break;
  867. case ACPI_NOTIFY_DEVICE_CHECK:
  868. /* device check : re-enumerate from parent bus */
  869. dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
  870. acpiphp_check_bridge(func->slot->bridge);
  871. break;
  872. case ACPI_NOTIFY_DEVICE_WAKE:
  873. /* wake event */
  874. dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
  875. break;
  876. case ACPI_NOTIFY_EJECT_REQUEST:
  877. /* request device eject */
  878. dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
  879. acpiphp_disable_slot(func->slot);
  880. break;
  881. default:
  882. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  883. break;
  884. }
  885. }
  886. static struct acpi_pci_driver acpi_pci_hp_driver = {
  887. .add = add_bridge,
  888. .remove = remove_bridge,
  889. };
  890. /**
  891. * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
  892. *
  893. */
  894. int __init acpiphp_glue_init(void)
  895. {
  896. int num;
  897. if (list_empty(&pci_root_buses))
  898. return -1;
  899. num = acpi_pci_register_driver(&acpi_pci_hp_driver);
  900. if (num <= 0)
  901. return -1;
  902. return 0;
  903. }
  904. /**
  905. * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
  906. *
  907. * This function frees all data allocated in acpiphp_glue_init()
  908. */
  909. void __exit acpiphp_glue_exit(void)
  910. {
  911. struct list_head *l1, *l2, *n1, *n2;
  912. struct acpiphp_bridge *bridge;
  913. struct acpiphp_slot *slot, *next;
  914. struct acpiphp_func *func;
  915. acpi_status status;
  916. list_for_each_safe (l1, n1, &bridge_list) {
  917. bridge = (struct acpiphp_bridge *)l1;
  918. slot = bridge->slots;
  919. while (slot) {
  920. next = slot->next;
  921. list_for_each_safe (l2, n2, &slot->funcs) {
  922. func = list_entry(l2, struct acpiphp_func, sibling);
  923. acpiphp_free_resource(&func->io_head);
  924. acpiphp_free_resource(&func->mem_head);
  925. acpiphp_free_resource(&func->p_mem_head);
  926. acpiphp_free_resource(&func->bus_head);
  927. status = acpi_remove_notify_handler(func->handle,
  928. ACPI_SYSTEM_NOTIFY,
  929. handle_hotplug_event_func);
  930. if (ACPI_FAILURE(status))
  931. err("failed to remove notify handler\n");
  932. kfree(func);
  933. }
  934. kfree(slot);
  935. slot = next;
  936. }
  937. status = acpi_remove_notify_handler(bridge->handle, ACPI_SYSTEM_NOTIFY,
  938. handle_hotplug_event_bridge);
  939. if (ACPI_FAILURE(status))
  940. err("failed to remove notify handler\n");
  941. acpiphp_free_resource(&bridge->io_head);
  942. acpiphp_free_resource(&bridge->mem_head);
  943. acpiphp_free_resource(&bridge->p_mem_head);
  944. acpiphp_free_resource(&bridge->bus_head);
  945. kfree(bridge);
  946. }
  947. acpi_pci_unregister_driver(&acpi_pci_hp_driver);
  948. }
  949. /**
  950. * acpiphp_get_num_slots - count number of slots in a system
  951. */
  952. int __init acpiphp_get_num_slots(void)
  953. {
  954. struct list_head *node;
  955. struct acpiphp_bridge *bridge;
  956. int num_slots;
  957. num_slots = 0;
  958. list_for_each (node, &bridge_list) {
  959. bridge = (struct acpiphp_bridge *)node;
  960. dbg("Bus%d %dslot(s)\n", bridge->bus, bridge->nr_slots);
  961. num_slots += bridge->nr_slots;
  962. }
  963. dbg("Total %dslots\n", num_slots);
  964. return num_slots;
  965. }
  966. #if 0
  967. /**
  968. * acpiphp_for_each_slot - call function for each slot
  969. * @fn: callback function
  970. * @data: context to be passed to callback function
  971. *
  972. */
  973. static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
  974. {
  975. struct list_head *node;
  976. struct acpiphp_bridge *bridge;
  977. struct acpiphp_slot *slot;
  978. int retval = 0;
  979. list_for_each (node, &bridge_list) {
  980. bridge = (struct acpiphp_bridge *)node;
  981. for (slot = bridge->slots; slot; slot = slot->next) {
  982. retval = fn(slot, data);
  983. if (!retval)
  984. goto err_exit;
  985. }
  986. }
  987. err_exit:
  988. return retval;
  989. }
  990. #endif
  991. /* search matching slot from id */
  992. struct acpiphp_slot *get_slot_from_id(int id)
  993. {
  994. struct list_head *node;
  995. struct acpiphp_bridge *bridge;
  996. struct acpiphp_slot *slot;
  997. list_for_each (node, &bridge_list) {
  998. bridge = (struct acpiphp_bridge *)node;
  999. for (slot = bridge->slots; slot; slot = slot->next)
  1000. if (slot->id == id)
  1001. return slot;
  1002. }
  1003. /* should never happen! */
  1004. err("%s: no object for id %d\n", __FUNCTION__, id);
  1005. WARN_ON(1);
  1006. return NULL;
  1007. }
  1008. /**
  1009. * acpiphp_enable_slot - power on slot
  1010. */
  1011. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1012. {
  1013. int retval;
  1014. down(&slot->crit_sect);
  1015. /* wake up all functions */
  1016. retval = power_on_slot(slot);
  1017. if (retval)
  1018. goto err_exit;
  1019. if (get_slot_status(slot) == ACPI_STA_ALL)
  1020. /* configure all functions */
  1021. retval = enable_device(slot);
  1022. err_exit:
  1023. up(&slot->crit_sect);
  1024. return retval;
  1025. }
  1026. /**
  1027. * acpiphp_disable_slot - power off slot
  1028. */
  1029. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1030. {
  1031. int retval = 0;
  1032. down(&slot->crit_sect);
  1033. /* unconfigure all functions */
  1034. retval = disable_device(slot);
  1035. if (retval)
  1036. goto err_exit;
  1037. /* power off all functions */
  1038. retval = power_off_slot(slot);
  1039. if (retval)
  1040. goto err_exit;
  1041. acpiphp_resource_sort_and_combine(&slot->bridge->io_head);
  1042. acpiphp_resource_sort_and_combine(&slot->bridge->mem_head);
  1043. acpiphp_resource_sort_and_combine(&slot->bridge->p_mem_head);
  1044. acpiphp_resource_sort_and_combine(&slot->bridge->bus_head);
  1045. dbg("Available resources:\n");
  1046. acpiphp_dump_resource(slot->bridge);
  1047. err_exit:
  1048. up(&slot->crit_sect);
  1049. return retval;
  1050. }
  1051. /*
  1052. * slot enabled: 1
  1053. * slot disabled: 0
  1054. */
  1055. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1056. {
  1057. unsigned int sta;
  1058. sta = get_slot_status(slot);
  1059. return (sta & ACPI_STA_ENABLED) ? 1 : 0;
  1060. }
  1061. /*
  1062. * latch closed: 1
  1063. * latch open: 0
  1064. */
  1065. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1066. {
  1067. unsigned int sta;
  1068. sta = get_slot_status(slot);
  1069. return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
  1070. }
  1071. /*
  1072. * adapter presence : 1
  1073. * absence : 0
  1074. */
  1075. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1076. {
  1077. unsigned int sta;
  1078. sta = get_slot_status(slot);
  1079. return (sta == 0) ? 0 : 1;
  1080. }
  1081. /*
  1082. * pci address (seg/bus/dev)
  1083. */
  1084. u32 acpiphp_get_address(struct acpiphp_slot *slot)
  1085. {
  1086. u32 address;
  1087. address = ((slot->bridge->seg) << 16) |
  1088. ((slot->bridge->bus) << 8) |
  1089. slot->device;
  1090. return address;
  1091. }