hw_phb.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. /* This file is part of the program psim.
  2. Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef _HW_PHB_C_
  15. #define _HW_PHB_C_
  16. #include "device_table.h"
  17. #include "hw_phb.h"
  18. #include "corefile.h"
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21. /* DEVICE
  22. phb - PCI Host Bridge
  23. DESCRIPTION
  24. PHB implements a model of the PCI-host bridge described in the PPCP
  25. document.
  26. For bridge devices, Open Firmware specifies that the <<ranges>>
  27. property be used to specify the mapping of address spaces between a
  28. bridges parent and child busses. This PHB model configures itsself
  29. according to the information specified in its ranges property. The
  30. <<ranges>> property is described in detail in the Open Firmware
  31. documentation.
  32. For DMA transfers, any access to a PCI address space which falls
  33. outside of the mapped memory space is assumed to be a transfer
  34. intended for the parent bus.
  35. PROPERTIES
  36. ranges = <my-phys-addr> <parent-phys-addr> <my-size> ... (required)
  37. Define a number of mappings from the parent bus to one of this
  38. devices PCI busses. The exact format of the <<parent-phys-addr>>
  39. is parent bus dependant. The format of <<my-phys-addr>> is
  40. described in the Open Firmware PCI bindings document (note that the
  41. address must be non-relocatable).
  42. #address-cells = 3 (required)
  43. Number of cells used by an Open Firmware PCI address. This
  44. property must be defined before specifying the <<ranges>> property.
  45. #size-cells = 2 (required)
  46. Number of cells used by an Open Firmware PCI size. This property
  47. must be defined before specifying the <<ranges>> property.
  48. EXAMPLES
  49. Enable tracing:
  50. | $ psim \
  51. | -t phb-device \
  52. Since device tree entries that are specified on the command line
  53. are added before most of the device tree has been built it is often
  54. necessary to explictly add certain device properties and thus
  55. ensure they are already present in the device tree. For the
  56. <<phb>> one such property is parent busses <<#address-cells>>.
  57. | -o '/#address-cells 1' \
  58. Create the PHB remembering to include the cell size properties:
  59. | -o '/phb@0x80000000/#address-cells 3' \
  60. | -o '/phb@0x80000000/#size-cells 2' \
  61. Specify that the memory address range <<0x80000000>> to
  62. <<0x8fffffff>> should map directly onto the PCI memory address
  63. space while the processor address range <<0xc0000000>> to
  64. <<0xc000ffff>> should map onto the PCI I/O address range starting
  65. at location zero:
  66. | -o '/phb@0x80000000/ranges \
  67. | nm0,0,0,80000000 0x80000000 0x10000000 \
  68. | ni0,0,0,0 0xc0000000 0x10000' \
  69. Insert a 4k <<nvram>> into slot zero of the PCI bus. Have it
  70. directly accessible in both the I/O (address <<0x100>>) and memory
  71. (address 0x80001000) spaces:
  72. | -o '/phb@0x80000000/nvram@0/assigned-addresses \
  73. | nm0,0,10,80001000 4096 \
  74. | ni0,0,14,100 4096'
  75. | -o '/phb@0x80000000/nvram@0/reg \
  76. | 0 0 \
  77. | i0,0,14,0 4096'
  78. | -o '/phb@0x80000000/nvram@0/alternate-reg \
  79. | 0 0 \
  80. | m0,0,10,0 4096'
  81. The <<assigned-address>> property corresponding to what (if it were
  82. implemented) be found in the config base registers while the
  83. <<reg>> and <<alternative-reg>> properties indicating the location
  84. of registers within each address space.
  85. Of the possible addresses, only the non-relocatable versions are
  86. used when attaching the device to the bus.
  87. BUGS
  88. The implementation of the PCI configuration space is left as an
  89. exercise for the reader. Such a restriction should only impact on
  90. systems wanting to dynamically configure devices on the PCI bus.
  91. The <<CHRP>> document specfies additional (optional) functionality
  92. of the primary PHB. The implementation of such functionality is
  93. left as an exercise for the reader.
  94. The Open Firmware PCI bus bindings document (rev 1.6 and 2.0) is
  95. unclear on the value of the "ss" bits for a 64bit memory address.
  96. The correct value, as used by this module, is 0b11.
  97. The Open Firmware PCI bus bindings document (rev 1.6) suggests that
  98. the register field of non-relocatable PCI address should be zero.
  99. Unfortunatly, PCI addresses specified in the <<assigned-addresses>>
  100. property must be both non-relocatable and have non-zero register
  101. fields.
  102. The unit-decode method is not inserting a bus number into any
  103. address that it decodes. Instead the bus-number is left as zero.
  104. Support for aliased memory and I/O addresses is left as an exercise
  105. for the reader.
  106. Support for interrupt-ack and special cycles are left as an
  107. exercise for the reader. One issue to consider when attempting
  108. this exercise is how to specify the address of the int-ack and
  109. special cycle register. Hint: <</8259-interrupt-ackowledge>> is
  110. the wrong answer.
  111. Children of this node can only use the client callback interface
  112. when attaching themselves to the <<phb>>.
  113. REFERENCES
  114. http://playground.sun.com/1275/home.html#OFDbusPCI
  115. */
  116. typedef struct _phb_space {
  117. core *map;
  118. core_map *readable;
  119. core_map *writeable;
  120. unsigned_word parent_base;
  121. int parent_space;
  122. unsigned_word my_base;
  123. int my_space;
  124. unsigned size;
  125. const char *name;
  126. } phb_space;
  127. typedef struct _hw_phb_device {
  128. phb_space space[nr_hw_phb_spaces];
  129. } hw_phb_device;
  130. static const char *
  131. hw_phb_decode_name(hw_phb_decode level)
  132. {
  133. switch (level) {
  134. case hw_phb_normal_decode: return "normal";
  135. case hw_phb_subtractive_decode: return "subtractive";
  136. case hw_phb_master_abort_decode: return "master-abort";
  137. default: return "invalid decode";
  138. }
  139. }
  140. static void
  141. hw_phb_init_address(device *me)
  142. {
  143. hw_phb_device *phb = device_data(me);
  144. /* check some basic properties */
  145. if (device_nr_address_cells(me) != 3)
  146. device_error(me, "incorrect #address-cells");
  147. if (device_nr_size_cells(me) != 2)
  148. device_error(me, "incorrect #size-cells");
  149. /* (re) initialize each PCI space */
  150. {
  151. hw_phb_spaces space_nr;
  152. for (space_nr = 0; space_nr < nr_hw_phb_spaces; space_nr++) {
  153. phb_space *pci_space = &phb->space[space_nr];
  154. core_init(pci_space->map);
  155. pci_space->size = 0;
  156. }
  157. }
  158. /* decode each of the ranges properties entering the information
  159. into the space table */
  160. {
  161. range_property_spec range;
  162. int ranges_entry;
  163. for (ranges_entry = 0;
  164. device_find_range_array_property(me, "ranges", ranges_entry,
  165. &range);
  166. ranges_entry++) {
  167. int my_attach_space;
  168. unsigned_word my_attach_address;
  169. int parent_attach_space;
  170. unsigned_word parent_attach_address;
  171. unsigned size;
  172. phb_space *pci_space;
  173. /* convert the addresses into something meaningful */
  174. device_address_to_attach_address(me, &range.child_address,
  175. &my_attach_space,
  176. &my_attach_address,
  177. me);
  178. device_address_to_attach_address(device_parent(me),
  179. &range.parent_address,
  180. &parent_attach_space,
  181. &parent_attach_address,
  182. me);
  183. device_size_to_attach_size(me, &range.size, &size, me);
  184. if (my_attach_space < 0 || my_attach_space >= nr_hw_phb_spaces)
  185. device_error(me, "ranges property contains an invalid address space");
  186. pci_space = &phb->space[my_attach_space];
  187. if (pci_space->size != 0)
  188. device_error(me, "ranges property contains duplicate mappings for %s address space",
  189. pci_space->name);
  190. pci_space->parent_base = parent_attach_address;
  191. pci_space->parent_space = parent_attach_space;
  192. pci_space->my_base = my_attach_address;
  193. pci_space->my_space = my_attach_space;
  194. pci_space->size = size;
  195. device_attach_address(device_parent(me),
  196. attach_callback,
  197. parent_attach_space, parent_attach_address, size,
  198. access_read_write_exec,
  199. me);
  200. DTRACE(phb, ("map %d:0x%lx to %s:0x%lx (0x%lx bytes)\n",
  201. (int)parent_attach_space,
  202. (unsigned long)parent_attach_address,
  203. pci_space->name,
  204. (unsigned long)my_attach_address,
  205. (unsigned long)size));
  206. }
  207. if (ranges_entry == 0) {
  208. device_error(me, "Missing or empty ranges property");
  209. }
  210. }
  211. }
  212. static void
  213. hw_phb_attach_address(device *me,
  214. attach_type type,
  215. int space,
  216. unsigned_word addr,
  217. unsigned nr_bytes,
  218. access_type access,
  219. device *client) /*callback/default*/
  220. {
  221. hw_phb_device *phb = device_data(me);
  222. phb_space *pci_space;
  223. /* sanity checks */
  224. if (space < 0 || space >= nr_hw_phb_spaces)
  225. device_error(me, "attach space (%d) specified by %s invalid",
  226. space, device_path(client));
  227. pci_space = &phb->space[space];
  228. if (addr + nr_bytes > pci_space->my_base + pci_space->size
  229. || addr < pci_space->my_base)
  230. device_error(me, "attach addr (0x%lx) specified by %s outside of bus address range",
  231. (unsigned long)addr, device_path(client));
  232. if ((hw_phb_decode)type != hw_phb_normal_decode
  233. && (hw_phb_decode)type != hw_phb_subtractive_decode)
  234. device_error(me, "attach type (%d) specified by %s invalid",
  235. type, device_path(client));
  236. /* attach it to the relevent bus */
  237. DTRACE(phb, ("attach %s - %s %s:0x%lx (0x%lx bytes)\n",
  238. device_path(client),
  239. hw_phb_decode_name(type),
  240. pci_space->name,
  241. (unsigned long)addr,
  242. (unsigned long)nr_bytes));
  243. core_attach(pci_space->map,
  244. type,
  245. space,
  246. access,
  247. addr,
  248. nr_bytes,
  249. client);
  250. }
  251. /* Extract/set various fields from a PCI unit address.
  252. Note: only the least significant 32 bits of each cell is used.
  253. Note: for PPC MSB is 0 while for PCI it is 31. */
  254. /* relocatable bit n */
  255. static unsigned
  256. extract_n(const device_unit *address)
  257. {
  258. return EXTRACTED32(address->cells[0], 0, 0);
  259. }
  260. static void
  261. set_n(device_unit *address)
  262. {
  263. BLIT32(address->cells[0], 0, 1);
  264. }
  265. /* prefetchable bit p */
  266. static unsigned
  267. extract_p(const device_unit *address)
  268. {
  269. ASSERT(address->nr_cells == 3);
  270. return EXTRACTED32(address->cells[0], 1, 1);
  271. }
  272. static void
  273. set_p(device_unit *address)
  274. {
  275. BLIT32(address->cells[0], 1, 1);
  276. }
  277. /* aliased bit t */
  278. static unsigned
  279. extract_t(const device_unit *address)
  280. {
  281. ASSERT(address->nr_cells == 3);
  282. return EXTRACTED32(address->cells[0], 2, 2);
  283. }
  284. static void
  285. set_t(device_unit *address)
  286. {
  287. BLIT32(address->cells[0], 2, 1);
  288. }
  289. /* space code ss */
  290. typedef enum {
  291. ss_config_code = 0,
  292. ss_io_code = 1,
  293. ss_32bit_memory_code = 2,
  294. ss_64bit_memory_code = 3,
  295. } ss_type;
  296. static ss_type
  297. extract_ss(const device_unit *address)
  298. {
  299. ASSERT(address->nr_cells == 3);
  300. return EXTRACTED32(address->cells[0], 6, 7);
  301. }
  302. static void
  303. set_ss(device_unit *address, ss_type val)
  304. {
  305. MBLIT32(address->cells[0], 6, 7, val);
  306. }
  307. /* bus number bbbbbbbb */
  308. #if 0
  309. static unsigned
  310. extract_bbbbbbbb(const device_unit *address)
  311. {
  312. ASSERT(address->nr_cells == 3);
  313. return EXTRACTED32(address->cells[0], 8, 15);
  314. }
  315. #endif
  316. #if 0
  317. static void
  318. set_bbbbbbbb(device_unit *address, unsigned val)
  319. {
  320. MBLIT32(address->cells[0], 8, 15, val);
  321. }
  322. #endif
  323. /* device number ddddd */
  324. static unsigned
  325. extract_ddddd(const device_unit *address)
  326. {
  327. ASSERT(address->nr_cells == 3);
  328. return EXTRACTED32(address->cells[0], 16, 20);
  329. }
  330. static void
  331. set_ddddd(device_unit *address, unsigned val)
  332. {
  333. MBLIT32(address->cells[0], 16, 20, val);
  334. }
  335. /* function number fff */
  336. static unsigned
  337. extract_fff(const device_unit *address)
  338. {
  339. ASSERT(address->nr_cells == 3);
  340. return EXTRACTED32(address->cells[0], 21, 23);
  341. }
  342. static void
  343. set_fff(device_unit *address, unsigned val)
  344. {
  345. MBLIT32(address->cells[0], 21, 23, val);
  346. }
  347. /* register number rrrrrrrr */
  348. static unsigned
  349. extract_rrrrrrrr(const device_unit *address)
  350. {
  351. ASSERT(address->nr_cells == 3);
  352. return EXTRACTED32(address->cells[0], 24, 31);
  353. }
  354. static void
  355. set_rrrrrrrr(device_unit *address, unsigned val)
  356. {
  357. MBLIT32(address->cells[0], 24, 31, val);
  358. }
  359. /* MSW of 64bit address hh..hh */
  360. static unsigned
  361. extract_hh_hh(const device_unit *address)
  362. {
  363. ASSERT(address->nr_cells == 3);
  364. return address->cells[1];
  365. }
  366. static void
  367. set_hh_hh(device_unit *address, unsigned val)
  368. {
  369. address->cells[2] = val;
  370. }
  371. /* LSW of 64bit address ll..ll */
  372. static unsigned
  373. extract_ll_ll(const device_unit *address)
  374. {
  375. ASSERT(address->nr_cells == 3);
  376. return address->cells[2];
  377. }
  378. static void
  379. set_ll_ll(device_unit *address, unsigned val)
  380. {
  381. address->cells[2] = val;
  382. }
  383. /* Convert PCI textual bus address into a device unit */
  384. static int
  385. hw_phb_unit_decode(device *me,
  386. const char *unit,
  387. device_unit *address)
  388. {
  389. char *end = NULL;
  390. const char *chp = unit;
  391. unsigned long val;
  392. if (device_nr_address_cells(me) != 3)
  393. device_error(me, "PCI bus should have #address-cells == 3");
  394. memset(address, 0, sizeof(*address));
  395. if (unit == NULL)
  396. return 0;
  397. address->nr_cells = 3;
  398. if (isxdigit(*chp)) {
  399. set_ss(address, ss_config_code);
  400. }
  401. else {
  402. /* non-relocatable? */
  403. if (*chp == 'n') {
  404. set_n(address);
  405. chp++;
  406. }
  407. /* address-space? */
  408. if (*chp == 'i') {
  409. set_ss(address, ss_io_code);
  410. chp++;
  411. }
  412. else if (*chp == 'm') {
  413. set_ss(address, ss_32bit_memory_code);
  414. chp++;
  415. }
  416. else if (*chp == 'x') {
  417. set_ss(address, ss_64bit_memory_code);
  418. chp++;
  419. }
  420. else
  421. device_error(me, "Problem parsing PCI address %s", unit);
  422. /* possible alias */
  423. if (*chp == 't') {
  424. if (extract_ss(address) == ss_64bit_memory_code)
  425. device_error(me, "Invalid alias bit in PCI address %s", unit);
  426. set_t(address);
  427. chp++;
  428. }
  429. /* possible p */
  430. if (*chp == 'p') {
  431. if (extract_ss(address) != ss_32bit_memory_code)
  432. device_error(me, "Invalid prefetchable bit (p) in PCI address %s",
  433. unit);
  434. set_p(address);
  435. chp++;
  436. }
  437. }
  438. /* required DD */
  439. if (!isxdigit(*chp))
  440. device_error(me, "Missing device number in PCI address %s", unit);
  441. val = strtoul(chp, &end, 16);
  442. if (chp == end)
  443. device_error(me, "Problem parsing device number in PCI address %s", unit);
  444. if ((val & 0x1f) != val)
  445. device_error(me, "Device number (0x%lx) out of range (0..0x1f) in PCI address %s",
  446. val, unit);
  447. set_ddddd(address, val);
  448. chp = end;
  449. /* For config space, the F is optional */
  450. if (extract_ss(address) == ss_config_code
  451. && (isspace(*chp) || *chp == '\0'))
  452. return chp - unit;
  453. /* function number F */
  454. if (*chp != ',')
  455. device_error(me, "Missing function number in PCI address %s", unit);
  456. chp++;
  457. val = strtoul(chp, &end, 10);
  458. if (chp == end)
  459. device_error(me, "Problem parsing function number in PCI address %s",
  460. unit);
  461. if ((val & 7) != val)
  462. device_error(me, "Function number (%ld) out of range (0..7) in PCI address %s",
  463. (long)val, unit);
  464. set_fff(address, val);
  465. chp = end;
  466. /* for config space, must be end */
  467. if (extract_ss(address) == ss_config_code) {
  468. if (!isspace(*chp) && *chp != '\0')
  469. device_error(me, "Problem parsing PCI config address %s",
  470. unit);
  471. return chp - unit;
  472. }
  473. /* register number RR */
  474. if (*chp != ',')
  475. device_error(me, "Missing register number in PCI address %s", unit);
  476. chp++;
  477. val = strtoul(chp, &end, 16);
  478. if (chp == end)
  479. device_error(me, "Problem parsing register number in PCI address %s",
  480. unit);
  481. switch (extract_ss(address)) {
  482. case ss_io_code:
  483. #if 0
  484. if (extract_n(address) && val != 0)
  485. device_error(me, "non-relocatable I/O register must be zero in PCI address %s", unit);
  486. else if (!extract_n(address)
  487. && val != 0x10 && val != 0x14 && val != 0x18
  488. && val != 0x1c && val != 0x20 && val != 0x24)
  489. device_error(me, "I/O register invalid in PCI address %s", unit);
  490. #endif
  491. break;
  492. case ss_32bit_memory_code:
  493. #if 0
  494. if (extract_n(address) && val != 0)
  495. device_error(me, "non-relocatable memory register must be zero in PCI address %s", unit);
  496. else if (!extract_n(address)
  497. && val != 0x10 && val != 0x14 && val != 0x18
  498. && val != 0x1c && val != 0x20 && val != 0x24 && val != 0x30)
  499. device_error(me, "I/O register (0x%lx) invalid in PCI address %s",
  500. val, unit);
  501. #endif
  502. break;
  503. case ss_64bit_memory_code:
  504. if (extract_n(address) && val != 0)
  505. device_error(me, "non-relocatable 32bit memory register must be zero in PCI address %s", unit);
  506. else if (!extract_n(address)
  507. && val != 0x10 && val != 0x18 && val != 0x20)
  508. device_error(me, "Register number (0x%lx) invalid in 64bit PCI address %s",
  509. val, unit);
  510. case ss_config_code:
  511. device_error(me, "internal error");
  512. }
  513. if ((val & 0xff) != val)
  514. device_error(me, "Register number (0x%lx) out of range (0..0xff) in PCI address %s",
  515. val, unit);
  516. set_rrrrrrrr(address, val);
  517. chp = end;
  518. /* address */
  519. if (*chp != ',')
  520. device_error(me, "Missing address in PCI address %s", unit);
  521. chp++;
  522. switch (extract_ss(address)) {
  523. case ss_io_code:
  524. case ss_32bit_memory_code:
  525. val = strtoul(chp, &end, 16);
  526. if (chp == end)
  527. device_error(me, "Problem parsing address in PCI address %s", unit);
  528. switch (extract_ss(address)) {
  529. case ss_io_code:
  530. if (extract_n(address) && extract_t(address)
  531. && (val & 1024) != val)
  532. device_error(me, "10bit aliased non-relocatable address (0x%lx) out of range in PCI address %s",
  533. val, unit);
  534. if (!extract_n(address) && extract_t(address)
  535. && (val & 0xffff) != val)
  536. device_error(me, "64k relocatable address (0x%lx) out of range in PCI address %s",
  537. val, unit);
  538. break;
  539. case ss_32bit_memory_code:
  540. if (extract_t(address) && (val & 0xfffff) != val)
  541. device_error(me, "1mb memory address (0x%lx) out of range in PCI address %s",
  542. val, unit);
  543. if (!extract_t(address) && (val & 0xffffffff) != val)
  544. device_error(me, "32bit memory address (0x%lx) out of range in PCI address %s",
  545. val, unit);
  546. break;
  547. case ss_64bit_memory_code:
  548. case ss_config_code:
  549. device_error(me, "internal error");
  550. }
  551. set_ll_ll(address, val);
  552. chp = end;
  553. break;
  554. case ss_64bit_memory_code:
  555. device_error(me, "64bit addresses unimplemented");
  556. set_hh_hh(address, val);
  557. set_ll_ll(address, val);
  558. break;
  559. case ss_config_code:
  560. device_error(me, "internal error");
  561. break;
  562. }
  563. /* finished? */
  564. if (!isspace(*chp) && *chp != '\0')
  565. device_error(me, "Problem parsing PCI address %s", unit);
  566. return chp - unit;
  567. }
  568. /* Convert PCI device unit into its corresponding textual
  569. representation */
  570. static int
  571. hw_phb_unit_encode(device *me,
  572. const device_unit *unit_address,
  573. char *buf,
  574. int sizeof_buf)
  575. {
  576. if (unit_address->nr_cells != 3)
  577. device_error(me, "Incorrect number of cells in PCI unit address");
  578. if (device_nr_address_cells(me) != 3)
  579. device_error(me, "PCI bus should have #address-cells == 3");
  580. if (extract_ss(unit_address) == ss_config_code
  581. && extract_fff(unit_address) == 0
  582. && extract_rrrrrrrr(unit_address) == 0
  583. && extract_hh_hh(unit_address) == 0
  584. && extract_ll_ll(unit_address) == 0) {
  585. /* DD - Configuration Space address */
  586. sprintf(buf, "%x",
  587. extract_ddddd(unit_address));
  588. }
  589. else if (extract_ss(unit_address) == ss_config_code
  590. && extract_fff(unit_address) != 0
  591. && extract_rrrrrrrr(unit_address) == 0
  592. && extract_hh_hh(unit_address) == 0
  593. && extract_ll_ll(unit_address) == 0) {
  594. /* DD,F - Configuration Space */
  595. sprintf(buf, "%x,%d",
  596. extract_ddddd(unit_address),
  597. extract_fff(unit_address));
  598. }
  599. else if (extract_ss(unit_address) == ss_io_code
  600. && extract_hh_hh(unit_address) == 0) {
  601. /* [n]i[t]DD,F,RR,NNNNNNNN - 32bit I/O space */
  602. sprintf(buf, "%si%s%x,%d,%x,%x",
  603. extract_n(unit_address) ? "n" : "",
  604. extract_t(unit_address) ? "t" : "",
  605. extract_ddddd(unit_address),
  606. extract_fff(unit_address),
  607. extract_rrrrrrrr(unit_address),
  608. extract_ll_ll(unit_address));
  609. }
  610. else if (extract_ss(unit_address) == ss_32bit_memory_code
  611. && extract_hh_hh(unit_address) == 0) {
  612. /* [n]m[t][p]DD,F,RR,NNNNNNNN - 32bit memory space */
  613. sprintf(buf, "%sm%s%s%x,%d,%x,%x",
  614. extract_n(unit_address) ? "n" : "",
  615. extract_t(unit_address) ? "t" : "",
  616. extract_p(unit_address) ? "p" : "",
  617. extract_ddddd(unit_address),
  618. extract_fff(unit_address),
  619. extract_rrrrrrrr(unit_address),
  620. extract_ll_ll(unit_address));
  621. }
  622. else if (extract_ss(unit_address) == ss_32bit_memory_code) {
  623. /* [n]x[p]DD,F,RR,NNNNNNNNNNNNNNNN - 64bit memory space */
  624. sprintf(buf, "%sx%s%x,%d,%x,%x%08x",
  625. extract_n(unit_address) ? "n" : "",
  626. extract_p(unit_address) ? "p" : "",
  627. extract_ddddd(unit_address),
  628. extract_fff(unit_address),
  629. extract_rrrrrrrr(unit_address),
  630. extract_hh_hh(unit_address),
  631. extract_ll_ll(unit_address));
  632. }
  633. else {
  634. device_error(me, "Invalid PCI unit address 0x%08lx 0x%08lx 0x%08lx",
  635. (unsigned long)unit_address->cells[0],
  636. (unsigned long)unit_address->cells[1],
  637. (unsigned long)unit_address->cells[2]);
  638. }
  639. if (strlen(buf) > sizeof_buf)
  640. error("buffer overflow");
  641. return strlen(buf);
  642. }
  643. static int
  644. hw_phb_address_to_attach_address(device *me,
  645. const device_unit *address,
  646. int *attach_space,
  647. unsigned_word *attach_address,
  648. device *client)
  649. {
  650. if (address->nr_cells != 3)
  651. device_error(me, "attach address has incorrect number of cells");
  652. if (address->cells[1] != 0)
  653. device_error(me, "64bit attach address unsupported");
  654. /* directly decode the address/space */
  655. *attach_address = address->cells[2];
  656. switch (extract_ss(address)) {
  657. case ss_config_code:
  658. *attach_space = hw_phb_config_space;
  659. break;
  660. case ss_io_code:
  661. *attach_space = hw_phb_io_space;
  662. break;
  663. case ss_32bit_memory_code:
  664. case ss_64bit_memory_code:
  665. *attach_space = hw_phb_memory_space;
  666. break;
  667. }
  668. /* if non-relocatable finished */
  669. if (extract_n(address))
  670. return 1;
  671. /* make memory and I/O addresses absolute */
  672. if (*attach_space == hw_phb_io_space
  673. || *attach_space == hw_phb_memory_space) {
  674. int reg_nr;
  675. reg_property_spec assigned;
  676. if (extract_ss(address) == ss_64bit_memory_code)
  677. device_error(me, "64bit memory address not unsuported");
  678. for (reg_nr = 0;
  679. device_find_reg_array_property(client, "assigned-addresses", reg_nr,
  680. &assigned);
  681. reg_nr++) {
  682. if (!extract_n(&assigned.address)
  683. || extract_rrrrrrrr(&assigned.address) == 0)
  684. device_error(me, "client %s has invalid assigned-address property",
  685. device_path(client));
  686. if (extract_rrrrrrrr(address) == extract_rrrrrrrr(&assigned.address)) {
  687. /* corresponding base register */
  688. if (extract_ss(address) != extract_ss(&assigned.address))
  689. device_error(me, "client %s has conflicting types for base register 0x%lx",
  690. device_path(client),
  691. (unsigned long)extract_rrrrrrrr(address));
  692. *attach_address += assigned.address.cells[2];
  693. return 0;
  694. }
  695. }
  696. device_error(me, "client %s missing base address register 0x%lx in assigned-addresses property",
  697. device_path(client),
  698. (unsigned long)extract_rrrrrrrr(address));
  699. }
  700. return 0;
  701. }
  702. static int
  703. hw_phb_size_to_attach_size(device *me,
  704. const device_unit *size,
  705. unsigned *nr_bytes,
  706. device *client)
  707. {
  708. if (size->nr_cells != 2)
  709. device_error(me, "size has incorrect number of cells");
  710. if (size->cells[0] != 0)
  711. device_error(me, "64bit size unsupported");
  712. *nr_bytes = size->cells[1];
  713. return size->cells[1];
  714. }
  715. static const phb_space *
  716. find_phb_space(hw_phb_device *phb,
  717. unsigned_word addr,
  718. unsigned nr_bytes)
  719. {
  720. hw_phb_spaces space;
  721. /* find the space that matches the address */
  722. for (space = 0; space < nr_hw_phb_spaces; space++) {
  723. phb_space *pci_space = &phb->space[space];
  724. if (addr >= pci_space->parent_base
  725. && (addr + nr_bytes) <= (pci_space->parent_base + pci_space->size)) {
  726. return pci_space;
  727. }
  728. }
  729. return NULL;
  730. }
  731. static unsigned_word
  732. map_phb_addr(const phb_space *space,
  733. unsigned_word addr)
  734. {
  735. return addr - space->parent_base + space->my_base;
  736. }
  737. static unsigned
  738. hw_phb_io_read_buffer(device *me,
  739. void *dest,
  740. int space,
  741. unsigned_word addr,
  742. unsigned nr_bytes,
  743. cpu *processor,
  744. unsigned_word cia)
  745. {
  746. hw_phb_device *phb = (hw_phb_device*)device_data(me);
  747. const phb_space *pci_space = find_phb_space(phb, addr, nr_bytes);
  748. unsigned_word bus_addr;
  749. if (pci_space == NULL)
  750. return 0;
  751. bus_addr = map_phb_addr(pci_space, addr);
  752. DTRACE(phb, ("io read - %d:0x%lx -> %s:0x%lx (%u bytes)\n",
  753. space, (unsigned long)addr, pci_space->name, (unsigned long)bus_addr,
  754. nr_bytes));
  755. return core_map_read_buffer(pci_space->readable,
  756. dest, bus_addr, nr_bytes);
  757. }
  758. static unsigned
  759. hw_phb_io_write_buffer(device *me,
  760. const void *source,
  761. int space,
  762. unsigned_word addr,
  763. unsigned nr_bytes,
  764. cpu *processor,
  765. unsigned_word cia)
  766. {
  767. hw_phb_device *phb = (hw_phb_device*)device_data(me);
  768. const phb_space *pci_space = find_phb_space(phb, addr, nr_bytes);
  769. unsigned_word bus_addr;
  770. if (pci_space == NULL)
  771. return 0;
  772. bus_addr = map_phb_addr(pci_space, addr);
  773. DTRACE(phb, ("io write - %d:0x%lx -> %s:0x%lx (%u bytes)\n",
  774. space, (unsigned long)addr, pci_space->name, (unsigned long)bus_addr,
  775. nr_bytes));
  776. return core_map_write_buffer(pci_space->writeable, source,
  777. bus_addr, nr_bytes);
  778. }
  779. static unsigned
  780. hw_phb_dma_read_buffer(device *me,
  781. void *dest,
  782. int space,
  783. unsigned_word addr,
  784. unsigned nr_bytes)
  785. {
  786. hw_phb_device *phb = (hw_phb_device*)device_data(me);
  787. const phb_space *pci_space;
  788. /* find the space */
  789. if (space != hw_phb_memory_space)
  790. device_error(me, "invalid dma address space %d", space);
  791. pci_space = &phb->space[space];
  792. /* check out the address */
  793. if ((addr >= pci_space->my_base
  794. && addr <= pci_space->my_base + pci_space->size)
  795. || (addr + nr_bytes >= pci_space->my_base
  796. && addr + nr_bytes <= pci_space->my_base + pci_space->size))
  797. device_error(me, "Do not support DMA into own bus");
  798. /* do it */
  799. DTRACE(phb, ("dma read - %s:0x%lx (%d bytes)\n",
  800. pci_space->name, (unsigned long)addr, nr_bytes));
  801. return device_dma_read_buffer(device_parent(me),
  802. dest, pci_space->parent_space,
  803. addr, nr_bytes);
  804. }
  805. static unsigned
  806. hw_phb_dma_write_buffer(device *me,
  807. const void *source,
  808. int space,
  809. unsigned_word addr,
  810. unsigned nr_bytes,
  811. int violate_read_only_section)
  812. {
  813. hw_phb_device *phb = (hw_phb_device*)device_data(me);
  814. const phb_space *pci_space;
  815. /* find the space */
  816. if (space != hw_phb_memory_space)
  817. device_error(me, "invalid dma address space %d", space);
  818. pci_space = &phb->space[space];
  819. /* check out the address */
  820. if ((addr >= pci_space->my_base
  821. && addr <= pci_space->my_base + pci_space->size)
  822. || (addr + nr_bytes >= pci_space->my_base
  823. && addr + nr_bytes <= pci_space->my_base + pci_space->size))
  824. device_error(me, "Do not support DMA into own bus");
  825. /* do it */
  826. DTRACE(phb, ("dma write - %s:0x%lx (%d bytes)\n",
  827. pci_space->name, (unsigned long)addr, nr_bytes));
  828. return device_dma_write_buffer(device_parent(me),
  829. source, pci_space->parent_space,
  830. addr, nr_bytes,
  831. violate_read_only_section);
  832. }
  833. static device_callbacks const hw_phb_callbacks = {
  834. { hw_phb_init_address, },
  835. { hw_phb_attach_address, },
  836. { hw_phb_io_read_buffer, hw_phb_io_write_buffer },
  837. { hw_phb_dma_read_buffer, hw_phb_dma_write_buffer },
  838. { NULL, }, /* interrupt */
  839. { hw_phb_unit_decode,
  840. hw_phb_unit_encode,
  841. hw_phb_address_to_attach_address,
  842. hw_phb_size_to_attach_size }
  843. };
  844. static void *
  845. hw_phb_create(const char *name,
  846. const device_unit *unit_address,
  847. const char *args)
  848. {
  849. /* create the descriptor */
  850. hw_phb_device *phb = ZALLOC(hw_phb_device);
  851. /* create the core maps now */
  852. hw_phb_spaces space_nr;
  853. for (space_nr = 0; space_nr < nr_hw_phb_spaces; space_nr++) {
  854. phb_space *pci_space = &phb->space[space_nr];
  855. pci_space->map = core_create();
  856. pci_space->readable = core_readable(pci_space->map);
  857. pci_space->writeable = core_writeable(pci_space->map);
  858. switch (space_nr) {
  859. case hw_phb_memory_space:
  860. pci_space->name = "memory";
  861. break;
  862. case hw_phb_io_space:
  863. pci_space->name = "I/O";
  864. break;
  865. case hw_phb_config_space:
  866. pci_space->name = "config";
  867. break;
  868. case hw_phb_special_space:
  869. pci_space->name = "special";
  870. break;
  871. default:
  872. error ("internal error");
  873. break;
  874. }
  875. }
  876. return phb;
  877. }
  878. const device_descriptor hw_phb_device_descriptor[] = {
  879. { "phb", hw_phb_create, &hw_phb_callbacks },
  880. { "pci", NULL, &hw_phb_callbacks },
  881. { NULL, },
  882. };
  883. #endif /* _HW_PHB_ */