pciehprm_nonacpi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * PCIEHPRM NONACPI: PHP Resource Manager for Non-ACPI/Legacy platform
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. * Copyright (C) 2003-2004 Intel Corporation
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  19. * NON INFRINGEMENT. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
  27. *
  28. */
  29. #include <linux/config.h>
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/types.h>
  33. #include <linux/sched.h>
  34. #include <linux/pci.h>
  35. #include <linux/init.h>
  36. #include <linux/slab.h>
  37. #include <asm/uaccess.h>
  38. #ifdef CONFIG_IA64
  39. #include <asm/iosapic.h>
  40. #endif
  41. #include "pciehp.h"
  42. #include "pciehprm.h"
  43. #include "pciehprm_nonacpi.h"
  44. void pciehprm_cleanup(void)
  45. {
  46. return;
  47. }
  48. int pciehprm_print_pirt(void)
  49. {
  50. return 0;
  51. }
  52. int pciehprm_get_physical_slot_number(struct controller *ctrl, u32 *sun, u8 busnum, u8 devnum)
  53. {
  54. *sun = (u8) (ctrl->first_slot);
  55. return 0;
  56. }
  57. static void print_pci_resource ( struct pci_resource *aprh)
  58. {
  59. struct pci_resource *res;
  60. for (res = aprh; res; res = res->next)
  61. dbg(" base= 0x%x length= 0x%x\n", res->base, res->length);
  62. }
  63. static void phprm_dump_func_res( struct pci_func *fun)
  64. {
  65. struct pci_func *func = fun;
  66. if (func->bus_head) {
  67. dbg(": BUS Resources:\n");
  68. print_pci_resource (func->bus_head);
  69. }
  70. if (func->io_head) {
  71. dbg(": IO Resources:\n");
  72. print_pci_resource (func->io_head);
  73. }
  74. if (func->mem_head) {
  75. dbg(": MEM Resources:\n");
  76. print_pci_resource (func->mem_head);
  77. }
  78. if (func->p_mem_head) {
  79. dbg(": PMEM Resources:\n");
  80. print_pci_resource (func->p_mem_head);
  81. }
  82. }
  83. static int phprm_get_used_resources (
  84. struct controller *ctrl,
  85. struct pci_func *func
  86. )
  87. {
  88. return pciehp_save_used_resources (ctrl, func, !DISABLE_CARD);
  89. }
  90. static int phprm_delete_resource(
  91. struct pci_resource **aprh,
  92. ulong base,
  93. ulong size)
  94. {
  95. struct pci_resource *res;
  96. struct pci_resource *prevnode;
  97. struct pci_resource *split_node;
  98. ulong tbase;
  99. pciehp_resource_sort_and_combine(aprh);
  100. for (res = *aprh; res; res = res->next) {
  101. if (res->base > base)
  102. continue;
  103. if ((res->base + res->length) < (base + size))
  104. continue;
  105. if (res->base < base) {
  106. tbase = base;
  107. if ((res->length - (tbase - res->base)) < size)
  108. continue;
  109. split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  110. if (!split_node)
  111. return -ENOMEM;
  112. split_node->base = res->base;
  113. split_node->length = tbase - res->base;
  114. res->base = tbase;
  115. res->length -= split_node->length;
  116. split_node->next = res->next;
  117. res->next = split_node;
  118. }
  119. if (res->length >= size) {
  120. split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  121. if (!split_node)
  122. return -ENOMEM;
  123. split_node->base = res->base + size;
  124. split_node->length = res->length - size;
  125. res->length = size;
  126. split_node->next = res->next;
  127. res->next = split_node;
  128. }
  129. if (*aprh == res) {
  130. *aprh = res->next;
  131. } else {
  132. prevnode = *aprh;
  133. while (prevnode->next != res)
  134. prevnode = prevnode->next;
  135. prevnode->next = res->next;
  136. }
  137. res->next = NULL;
  138. kfree(res);
  139. break;
  140. }
  141. return 0;
  142. }
  143. static int phprm_delete_resources(
  144. struct pci_resource **aprh,
  145. struct pci_resource *this
  146. )
  147. {
  148. struct pci_resource *res;
  149. for (res = this; res; res = res->next)
  150. phprm_delete_resource(aprh, res->base, res->length);
  151. return 0;
  152. }
  153. static int configure_existing_function(
  154. struct controller *ctrl,
  155. struct pci_func *func
  156. )
  157. {
  158. int rc;
  159. /* see how much resources the func has used. */
  160. rc = phprm_get_used_resources (ctrl, func);
  161. if (!rc) {
  162. /* subtract the resources used by the func from ctrl resources */
  163. rc = phprm_delete_resources (&ctrl->bus_head, func->bus_head);
  164. rc |= phprm_delete_resources (&ctrl->io_head, func->io_head);
  165. rc |= phprm_delete_resources (&ctrl->mem_head, func->mem_head);
  166. rc |= phprm_delete_resources (&ctrl->p_mem_head, func->p_mem_head);
  167. if (rc)
  168. warn("aCEF: cannot del used resources\n");
  169. } else
  170. err("aCEF: cannot get used resources\n");
  171. return rc;
  172. }
  173. static int pciehprm_delete_resource(
  174. struct pci_resource **aprh,
  175. ulong base,
  176. ulong size)
  177. {
  178. struct pci_resource *res;
  179. struct pci_resource *prevnode;
  180. struct pci_resource *split_node;
  181. ulong tbase;
  182. pciehp_resource_sort_and_combine(aprh);
  183. for (res = *aprh; res; res = res->next) {
  184. if (res->base > base)
  185. continue;
  186. if ((res->base + res->length) < (base + size))
  187. continue;
  188. if (res->base < base) {
  189. tbase = base;
  190. if ((res->length - (tbase - res->base)) < size)
  191. continue;
  192. split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  193. if (!split_node)
  194. return -ENOMEM;
  195. split_node->base = res->base;
  196. split_node->length = tbase - res->base;
  197. res->base = tbase;
  198. res->length -= split_node->length;
  199. split_node->next = res->next;
  200. res->next = split_node;
  201. }
  202. if (res->length >= size) {
  203. split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  204. if (!split_node)
  205. return -ENOMEM;
  206. split_node->base = res->base + size;
  207. split_node->length = res->length - size;
  208. res->length = size;
  209. split_node->next = res->next;
  210. res->next = split_node;
  211. }
  212. if (*aprh == res) {
  213. *aprh = res->next;
  214. } else {
  215. prevnode = *aprh;
  216. while (prevnode->next != res)
  217. prevnode = prevnode->next;
  218. prevnode->next = res->next;
  219. }
  220. res->next = NULL;
  221. kfree(res);
  222. break;
  223. }
  224. return 0;
  225. }
  226. static int bind_pci_resources_to_slots ( struct controller *ctrl)
  227. {
  228. struct pci_func *func, new_func;
  229. int busn = ctrl->slot_bus;
  230. int devn, funn;
  231. u32 vid;
  232. for (devn = 0; devn < 32; devn++) {
  233. for (funn = 0; funn < 8; funn++) {
  234. /*
  235. if (devn == ctrl->device && funn == ctrl->function)
  236. continue;
  237. */
  238. /* find out if this entry is for an occupied slot */
  239. vid = 0xFFFFFFFF;
  240. pci_bus_read_config_dword(ctrl->pci_dev->subordinate, PCI_DEVFN(devn, funn), PCI_VENDOR_ID, &vid);
  241. if (vid != 0xFFFFFFFF) {
  242. dbg("%s: vid = %x bus %x dev %x fun %x\n", __FUNCTION__,
  243. vid, busn, devn, funn);
  244. func = pciehp_slot_find(busn, devn, funn);
  245. dbg("%s: func = %p\n", __FUNCTION__,func);
  246. if (!func) {
  247. memset(&new_func, 0, sizeof(struct pci_func));
  248. new_func.bus = busn;
  249. new_func.device = devn;
  250. new_func.function = funn;
  251. new_func.is_a_board = 1;
  252. configure_existing_function(ctrl, &new_func);
  253. phprm_dump_func_res(&new_func);
  254. } else {
  255. configure_existing_function(ctrl, func);
  256. phprm_dump_func_res(func);
  257. }
  258. dbg("aCCF:existing PCI 0x%x Func ResourceDump\n", ctrl->bus);
  259. }
  260. }
  261. }
  262. return 0;
  263. }
  264. static void phprm_dump_ctrl_res( struct controller *ctlr)
  265. {
  266. struct controller *ctrl = ctlr;
  267. if (ctrl->bus_head) {
  268. dbg(": BUS Resources:\n");
  269. print_pci_resource (ctrl->bus_head);
  270. }
  271. if (ctrl->io_head) {
  272. dbg(": IO Resources:\n");
  273. print_pci_resource (ctrl->io_head);
  274. }
  275. if (ctrl->mem_head) {
  276. dbg(": MEM Resources:\n");
  277. print_pci_resource (ctrl->mem_head);
  278. }
  279. if (ctrl->p_mem_head) {
  280. dbg(": PMEM Resources:\n");
  281. print_pci_resource (ctrl->p_mem_head);
  282. }
  283. }
  284. /*
  285. * phprm_find_available_resources
  286. *
  287. * Finds available memory, IO, and IRQ resources for programming
  288. * devices which may be added to the system
  289. * this function is for hot plug ADD!
  290. *
  291. * returns 0 if success
  292. */
  293. int pciehprm_find_available_resources(struct controller *ctrl)
  294. {
  295. struct pci_func func;
  296. u32 rc;
  297. memset(&func, 0, sizeof(struct pci_func));
  298. func.bus = ctrl->bus;
  299. func.device = ctrl->device;
  300. func.function = ctrl->function;
  301. func.is_a_board = 1;
  302. /* Get resources for this PCI bridge */
  303. rc = pciehp_save_used_resources (ctrl, &func, !DISABLE_CARD);
  304. dbg("%s: pciehp_save_used_resources rc = %d\n", __FUNCTION__, rc);
  305. if (func.mem_head)
  306. func.mem_head->next = ctrl->mem_head;
  307. ctrl->mem_head = func.mem_head;
  308. if (func.p_mem_head)
  309. func.p_mem_head->next = ctrl->p_mem_head;
  310. ctrl->p_mem_head = func.p_mem_head;
  311. if (func.io_head)
  312. func.io_head->next = ctrl->io_head;
  313. ctrl->io_head = func.io_head;
  314. if(func.bus_head)
  315. func.bus_head->next = ctrl->bus_head;
  316. ctrl->bus_head = func.bus_head;
  317. if (ctrl->bus_head)
  318. pciehprm_delete_resource(&ctrl->bus_head, ctrl->pci_dev->subordinate->number, 1);
  319. dbg("%s:pre-Bind PCI 0x%x Ctrl Resource Dump\n", __FUNCTION__, ctrl->bus);
  320. phprm_dump_ctrl_res(ctrl);
  321. dbg("%s: before bind_pci_resources_to slots\n", __FUNCTION__);
  322. bind_pci_resources_to_slots (ctrl);
  323. dbg("%s:post-Bind PCI 0x%x Ctrl Resource Dump\n", __FUNCTION__, ctrl->bus);
  324. phprm_dump_ctrl_res(ctrl);
  325. return (rc);
  326. }
  327. int pciehprm_set_hpp(
  328. struct controller *ctrl,
  329. struct pci_func *func,
  330. u8 card_type)
  331. {
  332. u32 rc;
  333. u8 temp_byte;
  334. struct pci_bus lpci_bus, *pci_bus;
  335. unsigned int devfn;
  336. memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
  337. pci_bus = &lpci_bus;
  338. pci_bus->number = func->bus;
  339. devfn = PCI_DEVFN(func->device, func->function);
  340. temp_byte = 0x40; /* hard coded value for LT */
  341. if (card_type == PCI_HEADER_TYPE_BRIDGE) {
  342. /* set subordinate Latency Timer */
  343. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
  344. if (rc) {
  345. dbg("%s: set secondary LT error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__,
  346. func->bus, func->device, func->function);
  347. return rc;
  348. }
  349. }
  350. /* set base Latency Timer */
  351. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
  352. if (rc) {
  353. dbg("%s: set LT error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function);
  354. return rc;
  355. }
  356. /* set Cache Line size */
  357. temp_byte = 0x08; /* hard coded value for CLS */
  358. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
  359. if (rc) {
  360. dbg("%s: set CLS error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function);
  361. }
  362. /* set enable_perr */
  363. /* set enable_serr */
  364. return rc;
  365. }
  366. void pciehprm_enable_card(
  367. struct controller *ctrl,
  368. struct pci_func *func,
  369. u8 card_type)
  370. {
  371. u16 command, bcommand;
  372. struct pci_bus lpci_bus, *pci_bus;
  373. unsigned int devfn;
  374. int rc;
  375. memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
  376. pci_bus = &lpci_bus;
  377. pci_bus->number = func->bus;
  378. devfn = PCI_DEVFN(func->device, func->function);
  379. rc = pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &command);
  380. command |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR
  381. | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
  382. | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
  383. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
  384. if (card_type == PCI_HEADER_TYPE_BRIDGE) {
  385. rc = pci_bus_read_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, &bcommand);
  386. bcommand |= PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR
  387. | PCI_BRIDGE_CTL_NO_ISA;
  388. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, bcommand);
  389. }
  390. }
  391. static int legacy_pciehprm_init_pci(void)
  392. {
  393. return 0;
  394. }
  395. int pciehprm_init(enum php_ctlr_type ctrl_type)
  396. {
  397. int retval;
  398. switch (ctrl_type) {
  399. case PCI:
  400. retval = legacy_pciehprm_init_pci();
  401. break;
  402. default:
  403. retval = -ENODEV;
  404. break;
  405. }
  406. return retval;
  407. }