hw_opic.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  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_OPIC_C_
  15. #define _HW_OPIC_C_
  16. #include "device_table.h"
  17. #ifdef HAVE_STRING_H
  18. #include <string.h>
  19. #else
  20. #ifdef HAVE_STRINGS_H
  21. #include <strings.h>
  22. #endif
  23. #endif
  24. /* DEVICE
  25. opic - Open Programmable Interrupt Controller (OpenPIC)
  26. DESCRIPTION
  27. This device implements the core of the OpenPIC interrupt controller
  28. as described in the OpenPIC specification 1.2 and other related
  29. documents.
  30. The model includes:
  31. o Up to 2048 external interrupt sources
  32. o The four count down timers
  33. o The four interprocessor multicast interrupts
  34. o multiprocessor support
  35. o Full tracing to assist help debugging
  36. o Support for all variations of edge/level x high/low polarity.
  37. PROPERTIES
  38. reg = <address> <size> ... (required)
  39. Determine where the device lives in the parents address space. The
  40. first <<address>> <<size>> pair specifies the address of the
  41. interrupt destination unit (which might contain an interrupt source
  42. unit) while successive reg entries specify additional interrupt
  43. source units.
  44. Note that for an <<opic>> device attached to a <<pci>> bus, the
  45. first <<reg>> entry may need to be ignored it will be the address
  46. of the devices configuration registers.
  47. interrupt-ranges = <int-number> <range> ... (required)
  48. A list of pairs. Each pair corresponds to a block of interrupt
  49. source units (the address of which being specified by the
  50. corresponding reg tupple). <<int-number>> is the number of the
  51. first interrupt in the block while <<range>> is the number of
  52. interrupts in the block.
  53. timer-frequency = <integer> (optional)
  54. If present, specifies the default value of the timer frequency
  55. reporting register. By default a value of 1 HZ is used. The value
  56. is arbitrary, the timers are always updated once per machine cycle.
  57. vendor-identification = <integer> (optional)
  58. If present, specifies the value to be returned when the vendor
  59. identification register is read.
  60. EXAMPLES
  61. See the test suite directory:
  62. | psim-test/hw-opic
  63. BUGS
  64. For an OPIC controller attached to a PCI bus, it is not clear what
  65. the value of the <<reg>> and <<interrupt-ranges>> properties should
  66. be. In particular, the PCI firmware bindings require the first
  67. value of the <<reg>> property to specify the devices configuration
  68. address while the OpenPIC bindings require that same entry to
  69. specify the address of the Interrupt Delivery Unit. This
  70. implementation checks for and, if present, ignores any
  71. configuration address (and its corresponding <<interrupt-ranges>>
  72. entry).
  73. The OpenPIC specification requires the controller to be fair when
  74. distributing interrupts between processors. At present the
  75. algorithm used isn't fair. It is biased towards processor zero.
  76. The OpenPIC specification includes a 8259 pass through mode. This
  77. is not supported.
  78. REFERENCES
  79. PowerPC Multiprocessor Interrupt Controller (MPIC), January 19,
  80. 1996. Available from IBM.
  81. The Open Programmable Interrupt Controller (PIC) Register Interface
  82. Specification Revision 1.2. Issue Date: Opctober 1995. Available
  83. somewhere on AMD's web page (http://www.amd.com/)
  84. PowerPC Microprocessor Common Hardware Reference Platform (CHRP)
  85. System bindings to: IEEE Std 1275-1994 Standard for Boot
  86. (Initialization, Configuration) Firmware. Revision 1.2b (INTERIM
  87. DRAFT). April 22, 1996. Available on the Open Firmware web site
  88. http://playground.sun.com/p1275/.
  89. */
  90. /* forward types */
  91. typedef struct _hw_opic_device hw_opic_device;
  92. /* bounds */
  93. enum {
  94. max_nr_interrupt_sources = 2048,
  95. max_nr_interrupt_destinations = 32,
  96. max_nr_task_priorities = 16,
  97. };
  98. enum {
  99. opic_alignment = 16,
  100. };
  101. /* global configuration register */
  102. enum {
  103. gcr0_8259_bit = 0x20000000,
  104. gcr0_reset_bit = 0x80000000,
  105. };
  106. /* offsets and sizes */
  107. enum {
  108. idu_isu_base = 0x10000,
  109. sizeof_isu_register_block = 32,
  110. idu_per_processor_register_base = 0x20000,
  111. sizeof_idu_per_processor_register_block = 0x1000,
  112. idu_timer_base = 0x01100,
  113. sizeof_timer_register_block = 0x00040,
  114. };
  115. /* Interrupt sources */
  116. enum {
  117. isu_mask_bit = 0x80000000,
  118. isu_active_bit = 0x40000000,
  119. isu_multicast_bit = 0x20000000,
  120. isu_positive_polarity_bit = 0x00800000,
  121. isu_level_triggered_bit = 0x00400000,
  122. isu_priority_shift = 16,
  123. isu_vector_bits = 0x000000ff,
  124. };
  125. typedef struct _opic_interrupt_source {
  126. unsigned is_masked; /* left in place */
  127. unsigned is_multicast; /* left in place */
  128. unsigned is_positive_polarity; /* left in place */
  129. unsigned is_level_triggered; /* left in place */
  130. unsigned priority;
  131. unsigned vector;
  132. /* misc */
  133. int nr;
  134. unsigned destination;
  135. unsigned pending;
  136. unsigned in_service;
  137. } opic_interrupt_source;
  138. /* interrupt destinations (normally processors) */
  139. typedef struct _opic_interrupt_destination {
  140. int nr;
  141. unsigned base_priority;
  142. opic_interrupt_source *current_pending;
  143. opic_interrupt_source *current_in_service;
  144. unsigned bit;
  145. int init_port;
  146. int intr_port;
  147. } opic_interrupt_destination;
  148. /* address map descriptors */
  149. typedef struct _opic_isu_block { /* interrupt source unit block */
  150. int space;
  151. unsigned_word address;
  152. unsigned size;
  153. unsigned_cell int_number;
  154. unsigned_cell range;
  155. int reg;
  156. } opic_isu_block;
  157. typedef struct _opic_idu { /* interrupt delivery unit */
  158. int reg;
  159. int space;
  160. unsigned_word address;
  161. unsigned size;
  162. } opic_idu;
  163. typedef enum {
  164. /* bad */
  165. invalid_opic_register,
  166. /* interrupt source */
  167. interrupt_source_N_destination_register,
  168. interrupt_source_N_vector_priority_register,
  169. /* timers */
  170. timer_N_destination_register,
  171. timer_N_vector_priority_register,
  172. timer_N_base_count_register,
  173. timer_N_current_count_register,
  174. timer_frequency_reporting_register,
  175. /* inter-processor interrupts */
  176. ipi_N_vector_priority_register,
  177. ipi_N_dispatch_register,
  178. /* global configuration */
  179. spurious_vector_register,
  180. processor_init_register,
  181. vendor_identification_register,
  182. global_configuration_register_N,
  183. feature_reporting_register_N,
  184. /* per processor */
  185. end_of_interrupt_register_N,
  186. interrupt_acknowledge_register_N,
  187. current_task_priority_register_N,
  188. } opic_register;
  189. static const char *
  190. opic_register_name(opic_register type)
  191. {
  192. switch (type) {
  193. case invalid_opic_register: return "invalid_opic_register";
  194. case interrupt_source_N_destination_register: return "interrupt_source_N_destination_register";
  195. case interrupt_source_N_vector_priority_register: return "interrupt_source_N_vector_priority_register";
  196. case timer_N_destination_register: return "timer_N_destination_register";
  197. case timer_N_vector_priority_register: return "timer_N_vector_priority_register";
  198. case timer_N_base_count_register: return "timer_N_base_count_register";
  199. case timer_N_current_count_register: return "timer_N_current_count_register";
  200. case timer_frequency_reporting_register: return "timer_frequency_reporting_register";
  201. case ipi_N_vector_priority_register: return "ipi_N_vector_priority_register";
  202. case ipi_N_dispatch_register: return "ipi_N_dispatch_register";
  203. case spurious_vector_register: return "spurious_vector_register";
  204. case processor_init_register: return "processor_init_register";
  205. case vendor_identification_register: return "vendor_identification_register";
  206. case global_configuration_register_N: return "global_configuration_register_N";
  207. case feature_reporting_register_N: return "feature_reporting_register_N";
  208. case end_of_interrupt_register_N: return "end_of_interrupt_register_N";
  209. case interrupt_acknowledge_register_N: return "interrupt_acknowledge_register_N";
  210. case current_task_priority_register_N: return "current_task_priority_register_N";
  211. }
  212. return NULL;
  213. }
  214. /* timers */
  215. typedef struct _opic_timer {
  216. int nr;
  217. device *me; /* find my way home */
  218. hw_opic_device *opic; /* ditto */
  219. unsigned base_count;
  220. int inhibited;
  221. signed64 count; /* *ONLY* if inhibited */
  222. event_entry_tag timeout_event;
  223. opic_interrupt_source *interrupt_source;
  224. } opic_timer;
  225. /* the OPIC */
  226. struct _hw_opic_device {
  227. /* vendor id */
  228. unsigned vendor_identification;
  229. /* interrupt destinations - processors */
  230. int nr_interrupt_destinations;
  231. opic_interrupt_destination *interrupt_destination;
  232. unsigned sizeof_interrupt_destination;
  233. /* bogus interrupts */
  234. int spurious_vector;
  235. /* interrupt sources - external interrupt source units + extra internal ones */
  236. int nr_interrupt_sources;
  237. opic_interrupt_source *interrupt_source;
  238. unsigned sizeof_interrupt_source;
  239. /* external interrupts */
  240. int nr_external_interrupts;
  241. opic_interrupt_source *external_interrupt_source;
  242. /* inter-processor-interrupts */
  243. int nr_interprocessor_interrupts;
  244. opic_interrupt_source *interprocessor_interrupt_source;
  245. /* timers */
  246. int nr_timer_interrupts;
  247. opic_timer *timer;
  248. unsigned sizeof_timer;
  249. opic_interrupt_source *timer_interrupt_source;
  250. unsigned timer_frequency;
  251. /* init register */
  252. unsigned32 init;
  253. /* address maps */
  254. opic_idu idu;
  255. int nr_isu_blocks;
  256. opic_isu_block *isu_block;
  257. };
  258. static void
  259. hw_opic_init_data(device *me)
  260. {
  261. hw_opic_device *opic = (hw_opic_device*)device_data(me);
  262. int isb;
  263. int idu_reg;
  264. int nr_isu_blocks;
  265. int i;
  266. /* determine the first valid reg property entry (there could be
  267. leading reg entries with invalid (zero) size fields) and the
  268. number of isu entries found in the reg property. */
  269. idu_reg = 0;
  270. nr_isu_blocks = 0;
  271. while (1) {
  272. reg_property_spec unit;
  273. int attach_space;
  274. unsigned_word attach_address;
  275. unsigned attach_size;
  276. if (!device_find_reg_array_property(me, "reg", idu_reg + nr_isu_blocks,
  277. &unit))
  278. break;
  279. if (nr_isu_blocks > 0
  280. || (device_address_to_attach_address(device_parent(me), &unit.address,
  281. &attach_space, &attach_address,
  282. me)
  283. && device_size_to_attach_size(device_parent(me), &unit.size,
  284. &attach_size,
  285. me))) {
  286. /* we count any thing once we've found one valid address/size pair */
  287. nr_isu_blocks += 1;
  288. }
  289. else {
  290. idu_reg += 1;
  291. }
  292. }
  293. /* determine the number and location of the multiple interrupt
  294. source units and the single interrupt delivery unit */
  295. if (opic->isu_block == NULL) {
  296. int reg_nr;
  297. opic->nr_isu_blocks = nr_isu_blocks;
  298. opic->isu_block = zalloc(sizeof(opic_isu_block) * opic->nr_isu_blocks);
  299. isb = 0;
  300. reg_nr = idu_reg;
  301. while (isb < opic->nr_isu_blocks) {
  302. reg_property_spec reg;
  303. if (!device_find_reg_array_property(me, "reg", reg_nr, &reg))
  304. device_error(me, "reg property missing entry number %d", reg_nr);
  305. opic->isu_block[isb].reg = reg_nr;
  306. if (!device_address_to_attach_address(device_parent(me), &reg.address,
  307. &opic->isu_block[isb].space,
  308. &opic->isu_block[isb].address,
  309. me)
  310. || !device_size_to_attach_size(device_parent(me), &reg.size,
  311. &opic->isu_block[isb].size,
  312. me)) {
  313. device_error(me, "reg property entry %d invalid", reg_nr);
  314. }
  315. if (!device_find_integer_array_property(me, "interrupt-ranges",
  316. reg_nr * 2,
  317. &opic->isu_block[isb].int_number)
  318. || !device_find_integer_array_property(me, "interrupt-ranges",
  319. reg_nr * 2 + 1,
  320. &opic->isu_block[isb].range))
  321. device_error(me, "missing or invalid interrupt-ranges property entry %d", reg_nr);
  322. /* first reg entry specifies the address of both the IDU and the
  323. first set of ISU registers, adjust things accordingly */
  324. if (reg_nr == idu_reg) {
  325. opic->idu.reg = opic->isu_block[isb].reg;
  326. opic->idu.space = opic->isu_block[isb].space;
  327. opic->idu.address = opic->isu_block[isb].address;
  328. opic->idu.size = opic->isu_block[isb].size;
  329. opic->isu_block[isb].address += idu_isu_base;
  330. opic->isu_block[isb].size = opic->isu_block[isb].range * (16 + 16);
  331. }
  332. /* was this a valid reg entry? */
  333. if (opic->isu_block[isb].range == 0) {
  334. opic->nr_isu_blocks -= 1;
  335. }
  336. else {
  337. opic->nr_external_interrupts += opic->isu_block[isb].range;
  338. isb++;
  339. }
  340. reg_nr++;
  341. }
  342. }
  343. DTRACE(opic, ("interrupt source unit block - effective number of blocks %d\n",
  344. (int)opic->nr_isu_blocks));
  345. /* the number of other interrupts */
  346. opic->nr_interprocessor_interrupts = 4;
  347. opic->nr_timer_interrupts = 4;
  348. /* create space for the interrupt source registers */
  349. if (opic->interrupt_source != NULL) {
  350. memset(opic->interrupt_source, 0, opic->sizeof_interrupt_source);
  351. }
  352. else {
  353. opic->nr_interrupt_sources = (opic->nr_external_interrupts
  354. + opic->nr_interprocessor_interrupts
  355. + opic->nr_timer_interrupts);
  356. if (opic->nr_interrupt_sources > max_nr_interrupt_sources)
  357. device_error(me, "number of interrupt sources exceeded");
  358. opic->sizeof_interrupt_source = (sizeof(opic_interrupt_source)
  359. * opic->nr_interrupt_sources);
  360. opic->interrupt_source = zalloc(opic->sizeof_interrupt_source);
  361. opic->external_interrupt_source = opic->interrupt_source;
  362. opic->interprocessor_interrupt_source = (opic->external_interrupt_source
  363. + opic->nr_external_interrupts);
  364. opic->timer_interrupt_source = (opic->interprocessor_interrupt_source
  365. + opic->nr_interprocessor_interrupts);
  366. }
  367. for (i = 0; i < opic->nr_interrupt_sources; i++) {
  368. opic_interrupt_source *source = &opic->interrupt_source[i];
  369. source->nr = i;
  370. source->is_masked = isu_mask_bit;
  371. }
  372. DTRACE(opic, ("interrupt sources - external %d, timer %d, ipi %d, total %d\n",
  373. opic->nr_external_interrupts,
  374. opic->nr_timer_interrupts,
  375. opic->nr_interprocessor_interrupts,
  376. opic->nr_interrupt_sources));
  377. /* timers or interprocessor interrupts */
  378. if (opic->timer != NULL)
  379. memset(opic->timer, 0, opic->sizeof_timer);
  380. else {
  381. opic->nr_timer_interrupts = 4;
  382. opic->sizeof_timer = sizeof(opic_timer) * opic->nr_timer_interrupts;
  383. opic->timer = zalloc(opic->sizeof_timer);
  384. }
  385. for (i = 0; i < opic->nr_timer_interrupts; i++) {
  386. opic_timer *timer = &opic->timer[i];
  387. timer->nr = i;
  388. timer->me = me;
  389. timer->opic = opic;
  390. timer->inhibited = 1;
  391. timer->interrupt_source = &opic->timer_interrupt_source[i];
  392. }
  393. if (device_find_property(me, "timer-frequency"))
  394. opic->timer_frequency = device_find_integer_property(me, "timer-frequency");
  395. else
  396. opic->timer_frequency = 1;
  397. /* create space for the interrupt destination registers */
  398. if (opic->interrupt_destination != NULL) {
  399. memset(opic->interrupt_destination, 0, opic->sizeof_interrupt_destination);
  400. }
  401. else {
  402. opic->nr_interrupt_destinations = tree_find_integer_property(me, "/openprom/options/smp");
  403. opic->sizeof_interrupt_destination = (sizeof(opic_interrupt_destination)
  404. * opic->nr_interrupt_destinations);
  405. opic->interrupt_destination = zalloc(opic->sizeof_interrupt_destination);
  406. if (opic->nr_interrupt_destinations > max_nr_interrupt_destinations)
  407. device_error(me, "number of interrupt destinations exceeded");
  408. }
  409. for (i = 0; i < opic->nr_interrupt_destinations; i++) {
  410. opic_interrupt_destination *dest = &opic->interrupt_destination[i];
  411. dest->bit = (1 << i);
  412. dest->nr = i;
  413. dest->init_port = (device_interrupt_decode(me, "init0", output_port)
  414. + i);
  415. dest->intr_port = (device_interrupt_decode(me, "intr0", output_port)
  416. + i);
  417. dest->base_priority = max_nr_task_priorities - 1;
  418. }
  419. DTRACE(opic, ("interrupt destinations - total %d\n",
  420. (int)opic->nr_interrupt_destinations));
  421. /* verify and print out the ISU's */
  422. for (isb = 0; isb < opic->nr_isu_blocks; isb++) {
  423. unsigned correct_size;
  424. if ((opic->isu_block[isb].address % opic_alignment) != 0)
  425. device_error(me, "interrupt source unit %d address not aligned to %d byte boundary",
  426. isb, opic_alignment);
  427. correct_size = opic->isu_block[isb].range * sizeof_isu_register_block;
  428. if (opic->isu_block[isb].size != correct_size)
  429. device_error(me, "interrupt source unit %d (reg %d) has an incorrect size, should be 0x%x",
  430. isb, opic->isu_block[isb].reg, correct_size);
  431. DTRACE(opic, ("interrupt source unit block %ld - address %d:0x%lx, size 0x%lx, int-number %ld, range %ld\n",
  432. (long)isb,
  433. (int)opic->isu_block[isb].space,
  434. (unsigned long)opic->isu_block[isb].address,
  435. (unsigned long)opic->isu_block[isb].size,
  436. (long)opic->isu_block[isb].int_number,
  437. (long)opic->isu_block[isb].range));
  438. }
  439. /* verify and print out the IDU */
  440. {
  441. unsigned correct_size;
  442. unsigned alternate_size;
  443. if ((opic->idu.address % opic_alignment) != 0)
  444. device_error(me, "interrupt delivery unit not aligned to %d byte boundary",
  445. opic_alignment);
  446. correct_size = (idu_per_processor_register_base
  447. + (sizeof_idu_per_processor_register_block
  448. * opic->nr_interrupt_destinations));
  449. alternate_size = (idu_per_processor_register_base
  450. + (sizeof_idu_per_processor_register_block
  451. * max_nr_interrupt_destinations));
  452. if (opic->idu.size != correct_size
  453. && opic->idu.size != alternate_size)
  454. device_error(me, "interrupt delivery unit has incorrect size, should be 0x%x or 0x%x",
  455. correct_size, alternate_size);
  456. DTRACE(opic, ("interrupt delivery unit - address %d:0x%lx, size 0x%lx\n",
  457. (int)opic->idu.space,
  458. (unsigned long)opic->idu.address,
  459. (unsigned long)opic->idu.size));
  460. }
  461. /* initialize the init interrupts */
  462. opic->init = 0;
  463. /* vendor ident */
  464. if (device_find_property(me, "vendor-identification") != NULL)
  465. opic->vendor_identification = device_find_integer_property(me, "vendor-identification");
  466. else
  467. opic->vendor_identification = 0;
  468. /* misc registers */
  469. opic->spurious_vector = 0xff;
  470. }
  471. /* interrupt related actions */
  472. static void
  473. assert_interrupt(device *me,
  474. hw_opic_device *opic,
  475. opic_interrupt_destination *dest)
  476. {
  477. ASSERT(dest >= opic->interrupt_destination);
  478. ASSERT(dest < opic->interrupt_destination + opic->nr_interrupt_destinations);
  479. DTRACE(opic, ("assert interrupt - intr port %d\n", dest->intr_port));
  480. device_interrupt_event(me, dest->intr_port, 1, NULL, 0);
  481. }
  482. static void
  483. negate_interrupt(device *me,
  484. hw_opic_device *opic,
  485. opic_interrupt_destination *dest)
  486. {
  487. ASSERT(dest >= opic->interrupt_destination);
  488. ASSERT(dest < opic->interrupt_destination + opic->nr_interrupt_destinations);
  489. DTRACE(opic, ("negate interrupt - intr port %d\n", dest->intr_port));
  490. device_interrupt_event(me, dest->intr_port, 0, NULL, 0);
  491. }
  492. static int
  493. can_deliver(device *me,
  494. opic_interrupt_source *source,
  495. opic_interrupt_destination *dest)
  496. {
  497. return (source != NULL && dest != NULL
  498. && source->priority > dest->base_priority
  499. && (dest->current_in_service == NULL
  500. || source->priority > dest->current_in_service->priority));
  501. }
  502. static unsigned
  503. deliver_pending(device *me,
  504. hw_opic_device *opic,
  505. opic_interrupt_destination *dest)
  506. {
  507. ASSERT(can_deliver(me, dest->current_pending, dest));
  508. dest->current_in_service = dest->current_pending;
  509. dest->current_in_service->in_service |= dest->bit;
  510. if (!dest->current_pending->is_level_triggered) {
  511. if (dest->current_pending->is_multicast)
  512. dest->current_pending->pending &= ~dest->bit;
  513. else
  514. dest->current_pending->pending = 0;
  515. }
  516. dest->current_pending = NULL;
  517. negate_interrupt(me, opic, dest);
  518. return dest->current_in_service->vector;
  519. }
  520. typedef enum {
  521. pending_interrupt,
  522. in_service_interrupt,
  523. } interrupt_class;
  524. static opic_interrupt_source *
  525. find_interrupt_for_dest(device *me,
  526. hw_opic_device *opic,
  527. opic_interrupt_destination *dest,
  528. interrupt_class class)
  529. {
  530. int i;
  531. opic_interrupt_source *pending = NULL;
  532. for (i = 0; i < opic->nr_interrupt_sources; i++) {
  533. opic_interrupt_source *src = &opic->interrupt_source[i];
  534. /* is this a potential hit? */
  535. switch (class) {
  536. case in_service_interrupt:
  537. if ((src->in_service & dest->bit) == 0)
  538. continue;
  539. break;
  540. case pending_interrupt:
  541. if ((src->pending & dest->bit) == 0)
  542. continue;
  543. break;
  544. }
  545. /* see if it is the highest priority */
  546. if (pending == NULL)
  547. pending = src;
  548. else if (src->priority > pending->priority)
  549. pending = src;
  550. }
  551. return pending;
  552. }
  553. static opic_interrupt_destination *
  554. find_lowest_dest(device *me,
  555. hw_opic_device *opic,
  556. opic_interrupt_source *src)
  557. {
  558. int i;
  559. opic_interrupt_destination *lowest = NULL;
  560. for (i = 0; i < opic->nr_interrupt_destinations; i++) {
  561. opic_interrupt_destination *dest = &opic->interrupt_destination[i];
  562. if (src->destination & dest->bit) {
  563. if (dest->base_priority < src->priority) {
  564. if (lowest == NULL)
  565. lowest = dest;
  566. else if (lowest->base_priority > dest->base_priority)
  567. lowest = dest;
  568. else if (lowest->current_in_service != NULL
  569. && dest->current_in_service == NULL)
  570. lowest = dest; /* not doing anything */
  571. else if (lowest->current_in_service != NULL
  572. && dest->current_in_service != NULL
  573. && (lowest->current_in_service->priority
  574. > dest->current_in_service->priority))
  575. lowest = dest; /* less urgent */
  576. /* FIXME - need to be more fair */
  577. }
  578. }
  579. }
  580. return lowest;
  581. }
  582. static void
  583. handle_interrupt(device *me,
  584. hw_opic_device *opic,
  585. opic_interrupt_source *src,
  586. int asserted)
  587. {
  588. if (src->is_masked) {
  589. DTRACE(opic, ("interrupt %d - ignore masked\n", src->nr));
  590. }
  591. else if (src->is_multicast) {
  592. /* always try to deliver multicast interrupts - just easier */
  593. int i;
  594. ASSERT(!src->is_level_triggered);
  595. ASSERT(src->is_positive_polarity);
  596. ASSERT(asserted);
  597. for (i = 0; i < opic->nr_interrupt_destinations; i++) {
  598. opic_interrupt_destination *dest = &opic->interrupt_destination[i];
  599. if (src->destination & dest->bit) {
  600. if (src->pending & dest->bit) {
  601. DTRACE(opic, ("interrupt %d - multicast still pending to %d\n",
  602. src->nr, dest->nr));
  603. }
  604. else if (can_deliver(me, src, dest)) {
  605. dest->current_pending = src;
  606. src->pending |= dest->bit;
  607. assert_interrupt(me, opic, dest);
  608. DTRACE(opic, ("interrupt %d - multicast to %d\n",
  609. src->nr, dest->nr));
  610. }
  611. else {
  612. src->pending |= dest->bit;
  613. DTRACE(opic, ("interrupt %d - multicast pending to %d\n",
  614. src->nr, dest->nr));
  615. }
  616. }
  617. }
  618. }
  619. else if (src->is_level_triggered
  620. && src->is_positive_polarity
  621. && !asserted) {
  622. if (src->pending)
  623. DTRACE(opic, ("interrupt %d - ignore withdrawn (active high)\n",
  624. src->nr));
  625. else
  626. DTRACE(opic, ("interrupt %d - ignore low level (active high)\n",
  627. src->nr));
  628. ASSERT(!src->is_multicast);
  629. src->pending = 0;
  630. }
  631. else if (src->is_level_triggered
  632. && !src->is_positive_polarity
  633. && asserted) {
  634. if (src->pending)
  635. DTRACE(opic, ("interrupt %d - ignore withdrawn (active low)\n",
  636. src->nr));
  637. else
  638. DTRACE(opic, ("interrupt %d - ignore high level (active low)\n",
  639. src->nr));
  640. ASSERT(!src->is_multicast);
  641. src->pending = 0;
  642. }
  643. else if (!src->is_level_triggered
  644. && src->is_positive_polarity
  645. && !asserted) {
  646. DTRACE(opic, ("interrupt %d - ignore falling edge (positive edge trigered)\n",
  647. src->nr));
  648. }
  649. else if (!src->is_level_triggered
  650. && !src->is_positive_polarity
  651. && asserted) {
  652. DTRACE(opic, ("interrupt %d - ignore rising edge (negative edge trigered)\n",
  653. src->nr));
  654. }
  655. else if (src->in_service != 0) {
  656. /* leave the interrupt where it is */
  657. ASSERT(!src->is_multicast);
  658. ASSERT(src->pending == 0 || src->pending == src->in_service);
  659. src->pending = src->in_service;
  660. DTRACE(opic, ("interrupt %ld - ignore already in service to 0x%lx\n",
  661. (long)src->nr, (long)src->in_service));
  662. }
  663. else if (src->pending != 0) {
  664. DTRACE(opic, ("interrupt %ld - ignore still pending to 0x%lx\n",
  665. (long)src->nr, (long)src->pending));
  666. }
  667. else {
  668. /* delivery is needed */
  669. opic_interrupt_destination *dest = find_lowest_dest(me, opic, src);
  670. if (can_deliver(me, src, dest)) {
  671. dest->current_pending = src;
  672. src->pending = dest->bit;
  673. DTRACE(opic, ("interrupt %d - delivered to %d\n", src->nr, dest->nr));
  674. assert_interrupt(me, opic, dest);
  675. }
  676. else {
  677. src->pending = src->destination; /* any can take this */
  678. DTRACE(opic, ("interrupt %ld - pending to 0x%lx\n",
  679. (long)src->nr, (long)src->pending));
  680. }
  681. }
  682. }
  683. static unsigned
  684. do_interrupt_acknowledge_register_N_read(device *me,
  685. hw_opic_device *opic,
  686. int dest_nr)
  687. {
  688. opic_interrupt_destination *dest = &opic->interrupt_destination[dest_nr];
  689. unsigned vector;
  690. ASSERT(dest_nr >= 0 && dest_nr < opic->nr_interrupt_destinations);
  691. ASSERT(dest_nr == dest->nr);
  692. /* try the current pending */
  693. if (can_deliver(me, dest->current_pending, dest)) {
  694. ASSERT(dest->current_pending->pending & dest->bit);
  695. vector = deliver_pending(me, opic, dest);
  696. DTRACE(opic, ("interrupt ack %d - entering %d (pending) - vector %d (%d), priority %d\n",
  697. dest->nr,
  698. dest->current_in_service->nr,
  699. dest->current_in_service->vector, vector,
  700. dest->current_in_service->priority));
  701. }
  702. else {
  703. /* try for something else */
  704. dest->current_pending = find_interrupt_for_dest(me, opic, dest, pending_interrupt);
  705. if (can_deliver(me, dest->current_pending, dest)) {
  706. vector = deliver_pending(me, opic, dest);
  707. DTRACE(opic, ("interrupt ack %d - entering %d (not pending) - vector %d (%d), priority %d\n",
  708. dest->nr,
  709. dest->current_in_service->nr,
  710. dest->current_in_service->vector, vector,
  711. dest->current_in_service->priority));
  712. }
  713. else {
  714. dest->current_pending = NULL;
  715. vector = opic->spurious_vector;
  716. DTRACE(opic, ("interrupt ack %d - spurious interrupt %d\n",
  717. dest->nr, vector));
  718. }
  719. }
  720. return vector;
  721. }
  722. static void
  723. do_end_of_interrupt_register_N_write(device *me,
  724. hw_opic_device *opic,
  725. int dest_nr,
  726. unsigned reg)
  727. {
  728. opic_interrupt_destination *dest = &opic->interrupt_destination[dest_nr];
  729. ASSERT(dest_nr >= 0 && dest_nr < opic->nr_interrupt_destinations);
  730. ASSERT(dest_nr == dest->nr);
  731. /* check the value written is zero */
  732. if (reg != 0) {
  733. DTRACE(opic, ("eoi %d - ignoring nonzero value\n", dest->nr));
  734. }
  735. /* user doing wierd things? */
  736. if (dest->current_in_service == NULL) {
  737. DTRACE(opic, ("eoi %d - strange, no current interrupt\n", dest->nr));
  738. return;
  739. }
  740. /* an internal stuff up? */
  741. if (!(dest->current_in_service->in_service & dest->bit)) {
  742. device_error(me, "eoi %d - current interrupt not in service", dest->nr);
  743. }
  744. /* find what was probably the previous in service interrupt */
  745. dest->current_in_service->in_service &= ~dest->bit;
  746. DTRACE(opic, ("eoi %d - ending %d - priority %d, vector %d\n",
  747. dest->nr,
  748. dest->current_in_service->nr,
  749. dest->current_in_service->priority,
  750. dest->current_in_service->vector));
  751. dest->current_in_service = find_interrupt_for_dest(me, opic, dest, in_service_interrupt);
  752. if (dest->current_in_service != NULL)
  753. DTRACE(opic, ("eoi %d - resuming %d - priority %d, vector %d\n",
  754. dest->nr,
  755. dest->current_in_service->nr,
  756. dest->current_in_service->priority,
  757. dest->current_in_service->vector));
  758. else
  759. DTRACE(opic, ("eoi %d - resuming none\n", dest->nr));
  760. /* check to see if that shouldn't be interrupted */
  761. dest->current_pending = find_interrupt_for_dest(me, opic, dest, pending_interrupt);
  762. if (can_deliver(me, dest->current_pending, dest)) {
  763. ASSERT(dest->current_pending->pending & dest->bit);
  764. assert_interrupt(me, opic, dest);
  765. }
  766. else {
  767. dest->current_pending = NULL;
  768. }
  769. }
  770. static void
  771. decode_opic_address(device *me,
  772. hw_opic_device *opic,
  773. int space,
  774. unsigned_word address,
  775. unsigned nr_bytes,
  776. opic_register *type,
  777. int *index)
  778. {
  779. int isb = 0;
  780. /* is the size valid? */
  781. if (nr_bytes != 4) {
  782. *type = invalid_opic_register;
  783. *index = -1;
  784. return;
  785. }
  786. /* try for a per-processor register within the interrupt delivery
  787. unit */
  788. if (space == opic->idu.space
  789. && address >= (opic->idu.address + idu_per_processor_register_base)
  790. && address < (opic->idu.address + idu_per_processor_register_base
  791. + (sizeof_idu_per_processor_register_block
  792. * opic->nr_interrupt_destinations))) {
  793. unsigned_word block_offset = (address
  794. - opic->idu.address
  795. - idu_per_processor_register_base);
  796. unsigned_word offset = block_offset % sizeof_idu_per_processor_register_block;
  797. *index = block_offset / sizeof_idu_per_processor_register_block;
  798. switch (offset) {
  799. case 0x040:
  800. *type = ipi_N_dispatch_register;
  801. *index = 0;
  802. break;
  803. case 0x050:
  804. *type = ipi_N_dispatch_register;
  805. *index = 1;
  806. break;
  807. case 0x060:
  808. *type = ipi_N_dispatch_register;
  809. *index = 2;
  810. break;
  811. case 0x070:
  812. *type = ipi_N_dispatch_register;
  813. *index = 3;
  814. break;
  815. case 0x080:
  816. *type = current_task_priority_register_N;
  817. break;
  818. case 0x0a0:
  819. *type = interrupt_acknowledge_register_N;
  820. break;
  821. case 0x0b0:
  822. *type = end_of_interrupt_register_N;
  823. break;
  824. default:
  825. *type = invalid_opic_register;
  826. break;
  827. }
  828. DTRACE(opic, ("per-processor register %d:0x%lx - %s[%d]\n",
  829. space, (unsigned long)address,
  830. opic_register_name(*type),
  831. *index));
  832. return;
  833. }
  834. /* try for an interrupt source unit */
  835. for (isb = 0; isb < opic->nr_isu_blocks; isb++) {
  836. if (opic->isu_block[isb].space == space
  837. && address >= opic->isu_block[isb].address
  838. && address < (opic->isu_block[isb].address + opic->isu_block[isb].size)) {
  839. unsigned_word block_offset = address - opic->isu_block[isb].address;
  840. unsigned_word offset = block_offset % sizeof_isu_register_block;
  841. *index = (opic->isu_block[isb].int_number
  842. + (block_offset / sizeof_isu_register_block));
  843. switch (offset) {
  844. case 0x00:
  845. *type = interrupt_source_N_vector_priority_register;
  846. break;
  847. case 0x10:
  848. *type = interrupt_source_N_destination_register;
  849. break;
  850. default:
  851. *type = invalid_opic_register;
  852. break;
  853. }
  854. DTRACE(opic, ("isu register %d:0x%lx - %s[%d]\n",
  855. space, (unsigned long)address,
  856. opic_register_name(*type),
  857. *index));
  858. return;
  859. }
  860. }
  861. /* try for a timer */
  862. if (space == opic->idu.space
  863. && address >= (opic->idu.address + idu_timer_base)
  864. && address < (opic->idu.address + idu_timer_base
  865. + opic->nr_timer_interrupts * sizeof_timer_register_block)) {
  866. unsigned_word offset = address % sizeof_timer_register_block;
  867. *index = ((address - opic->idu.address - idu_timer_base)
  868. / sizeof_timer_register_block);
  869. switch (offset) {
  870. case 0x00:
  871. *type = timer_N_current_count_register;
  872. break;
  873. case 0x10:
  874. *type = timer_N_base_count_register;
  875. break;
  876. case 0x20:
  877. *type = timer_N_vector_priority_register;
  878. break;
  879. case 0x30:
  880. *type = timer_N_destination_register;
  881. break;
  882. default:
  883. *type = invalid_opic_register;
  884. break;
  885. }
  886. DTRACE(opic, ("timer register %d:0x%lx - %s[%d]\n",
  887. space, (unsigned long)address,
  888. opic_register_name(*type),
  889. *index));
  890. return;
  891. }
  892. /* finally some other misc global register */
  893. if (space == opic->idu.space
  894. && address >= opic->idu.address
  895. && address < opic->idu.address + opic->idu.size) {
  896. unsigned_word block_offset = address - opic->idu.address;
  897. switch (block_offset) {
  898. case 0x010f0:
  899. *type = timer_frequency_reporting_register;
  900. *index = -1;
  901. break;
  902. case 0x010e0:
  903. *type = spurious_vector_register;
  904. *index = -1;
  905. break;
  906. case 0x010d0:
  907. case 0x010c0:
  908. case 0x010b0:
  909. case 0x010a0:
  910. *type = ipi_N_vector_priority_register;
  911. *index = (block_offset - 0x010a0) / 16;
  912. break;
  913. case 0x01090:
  914. *type = processor_init_register;
  915. *index = -1;
  916. break;
  917. case 0x01080:
  918. *type = vendor_identification_register;
  919. *index = -1;
  920. break;
  921. case 0x01020:
  922. *type = global_configuration_register_N;
  923. *index = 0;
  924. break;
  925. case 0x01000:
  926. *type = feature_reporting_register_N;
  927. *index = 0;
  928. break;
  929. default:
  930. *type = invalid_opic_register;
  931. *index = -1;
  932. break;
  933. }
  934. DTRACE(opic, ("global register %d:0x%lx - %s[%d]\n",
  935. space, (unsigned long)address,
  936. opic_register_name(*type),
  937. *index));
  938. return;
  939. }
  940. /* nothing matched */
  941. *type = invalid_opic_register;
  942. DTRACE(opic, ("invalid register %d:0x%lx\n",
  943. space, (unsigned long)address));
  944. return;
  945. }
  946. /* Processor init register:
  947. The bits in this register (one per processor) are directly wired to
  948. output "init" interrupt ports. */
  949. static unsigned
  950. do_processor_init_register_read(device *me,
  951. hw_opic_device *opic)
  952. {
  953. unsigned reg = opic->init;
  954. DTRACE(opic, ("processor init register - read 0x%lx\n",
  955. (long)reg));
  956. return reg;
  957. }
  958. static void
  959. do_processor_init_register_write(device *me,
  960. hw_opic_device *opic,
  961. unsigned reg)
  962. {
  963. int i;
  964. for (i = 0; i < opic->nr_interrupt_destinations; i++) {
  965. opic_interrupt_destination *dest = &opic->interrupt_destination[i];
  966. if ((reg & dest->bit) != (opic->init & dest->bit)) {
  967. if (reg & dest->bit) {
  968. DTRACE(opic, ("processor init register - write 0x%lx - asserting init%d\n",
  969. (long)reg, i));
  970. opic->init |= dest->bit;
  971. device_interrupt_event(me, dest->init_port, 1, NULL, 0);
  972. }
  973. else {
  974. DTRACE(opic, ("processor init register - write 0x%lx - negating init%d\n",
  975. (long)reg, i));
  976. opic->init &= ~dest->bit;
  977. device_interrupt_event(me, dest->init_port, 0, NULL, 0);
  978. }
  979. }
  980. }
  981. }
  982. /* Interrupt Source Vector/Priority Register: */
  983. static unsigned
  984. read_vector_priority_register(device *me,
  985. hw_opic_device *opic,
  986. opic_interrupt_source *interrupt,
  987. const char *reg_name,
  988. int reg_index)
  989. {
  990. unsigned reg;
  991. reg = 0;
  992. reg |= interrupt->is_masked;
  993. reg |= (interrupt->in_service || interrupt->pending
  994. ? isu_active_bit : 0); /* active */
  995. reg |= interrupt->is_multicast;
  996. reg |= interrupt->is_positive_polarity;
  997. reg |= interrupt->is_level_triggered; /* sense? */
  998. reg |= interrupt->priority << isu_priority_shift;
  999. reg |= interrupt->vector;
  1000. DTRACE(opic, ("%s %d vector/priority register - read 0x%lx\n",
  1001. reg_name, reg_index, (unsigned long)reg));
  1002. return reg;
  1003. }
  1004. static unsigned
  1005. do_interrupt_source_N_vector_priority_register_read(device *me,
  1006. hw_opic_device *opic,
  1007. int index)
  1008. {
  1009. unsigned reg;
  1010. ASSERT(index < opic->nr_external_interrupts);
  1011. reg = read_vector_priority_register(me, opic,
  1012. &opic->interrupt_source[index],
  1013. "interrupt source", index);
  1014. return reg;
  1015. }
  1016. static void
  1017. write_vector_priority_register(device *me,
  1018. hw_opic_device *opic,
  1019. opic_interrupt_source *interrupt,
  1020. unsigned reg,
  1021. const char *reg_name,
  1022. int reg_index)
  1023. {
  1024. interrupt->is_masked = (reg & isu_mask_bit);
  1025. interrupt->is_multicast = (reg & isu_multicast_bit);
  1026. interrupt->is_positive_polarity = (reg & isu_positive_polarity_bit);
  1027. interrupt->is_level_triggered = (reg & isu_level_triggered_bit);
  1028. interrupt->priority = ((reg >> isu_priority_shift)
  1029. % max_nr_task_priorities);
  1030. interrupt->vector = (reg & isu_vector_bits);
  1031. DTRACE(opic, ("%s %d vector/priority register - write 0x%lx - %s%s%s-polarity, %s-triggered, priority %ld vector %ld\n",
  1032. reg_name,
  1033. reg_index,
  1034. (unsigned long)reg,
  1035. interrupt->is_masked ? "masked, " : "",
  1036. interrupt->is_multicast ? "multicast, " : "",
  1037. interrupt->is_positive_polarity ? "positive" : "negative",
  1038. interrupt->is_level_triggered ? "level" : "edge",
  1039. (long)interrupt->priority,
  1040. (long)interrupt->vector));
  1041. }
  1042. static void
  1043. do_interrupt_source_N_vector_priority_register_write(device *me,
  1044. hw_opic_device *opic,
  1045. int index,
  1046. unsigned reg)
  1047. {
  1048. ASSERT(index < opic->nr_external_interrupts);
  1049. reg &= ~isu_multicast_bit; /* disable multicast */
  1050. write_vector_priority_register(me, opic,
  1051. &opic->interrupt_source[index],
  1052. reg, "interrupt source", index);
  1053. }
  1054. /* Interrupt Source Destination Register: */
  1055. static unsigned
  1056. read_destination_register(device *me,
  1057. hw_opic_device *opic,
  1058. opic_interrupt_source *interrupt,
  1059. const char *reg_name,
  1060. int reg_index)
  1061. {
  1062. unsigned long reg;
  1063. reg = interrupt->destination;
  1064. DTRACE(opic, ("%s %d destination register - read 0x%lx\n",
  1065. reg_name, reg_index, reg));
  1066. return reg;
  1067. }
  1068. static unsigned
  1069. do_interrupt_source_N_destination_register_read(device *me,
  1070. hw_opic_device *opic,
  1071. int index)
  1072. {
  1073. unsigned reg;
  1074. ASSERT(index < opic->nr_external_interrupts);
  1075. reg = read_destination_register(me, opic, &opic->external_interrupt_source[index],
  1076. "interrupt source", index);
  1077. return reg;
  1078. }
  1079. static void
  1080. write_destination_register(device *me,
  1081. hw_opic_device *opic,
  1082. opic_interrupt_source *interrupt,
  1083. unsigned reg,
  1084. const char *reg_name,
  1085. int reg_index)
  1086. {
  1087. reg &= (1 << opic->nr_interrupt_destinations) - 1; /* mask out invalid */
  1088. DTRACE(opic, ("%s %d destination register - write 0x%x\n",
  1089. reg_name, reg_index, reg));
  1090. interrupt->destination = reg;
  1091. }
  1092. static void
  1093. do_interrupt_source_N_destination_register_write(device *me,
  1094. hw_opic_device *opic,
  1095. int index,
  1096. unsigned reg)
  1097. {
  1098. ASSERT(index < opic->nr_external_interrupts);
  1099. write_destination_register(me, opic, &opic->external_interrupt_source[index],
  1100. reg, "interrupt source", index);
  1101. }
  1102. /* Spurious vector register: */
  1103. static unsigned
  1104. do_spurious_vector_register_read(device *me,
  1105. hw_opic_device *opic)
  1106. {
  1107. unsigned long reg = opic->spurious_vector;
  1108. DTRACE(opic, ("spurious vector register - read 0x%lx\n", reg));
  1109. return reg;
  1110. }
  1111. static void
  1112. do_spurious_vector_register_write(device *me,
  1113. hw_opic_device *opic,
  1114. unsigned reg)
  1115. {
  1116. reg &= 0xff; /* mask off invalid */
  1117. DTRACE(opic, ("spurious vector register - write 0x%x\n", reg));
  1118. opic->spurious_vector = reg;
  1119. }
  1120. /* current task priority register: */
  1121. static unsigned
  1122. do_current_task_priority_register_N_read(device *me,
  1123. hw_opic_device *opic,
  1124. int index)
  1125. {
  1126. opic_interrupt_destination *interrupt_destination = &opic->interrupt_destination[index];
  1127. unsigned reg;
  1128. ASSERT(index >= 0 && index < opic->nr_interrupt_destinations);
  1129. reg = interrupt_destination->base_priority;
  1130. DTRACE(opic, ("current task priority register %d - read 0x%x\n", index, reg));
  1131. return reg;
  1132. }
  1133. static void
  1134. do_current_task_priority_register_N_write(device *me,
  1135. hw_opic_device *opic,
  1136. int index,
  1137. unsigned reg)
  1138. {
  1139. opic_interrupt_destination *interrupt_destination = &opic->interrupt_destination[index];
  1140. ASSERT(index >= 0 && index < opic->nr_interrupt_destinations);
  1141. reg %= max_nr_task_priorities;
  1142. DTRACE(opic, ("current task priority register %d - write 0x%x\n", index, reg));
  1143. interrupt_destination->base_priority = reg;
  1144. }
  1145. /* Timer Frequency Reporting Register: */
  1146. static unsigned
  1147. do_timer_frequency_reporting_register_read(device *me,
  1148. hw_opic_device *opic)
  1149. {
  1150. unsigned reg;
  1151. reg = opic->timer_frequency;
  1152. DTRACE(opic, ("timer frequency reporting register - read 0x%x\n", reg));
  1153. return reg;
  1154. }
  1155. static void
  1156. do_timer_frequency_reporting_register_write(device *me,
  1157. hw_opic_device *opic,
  1158. unsigned reg)
  1159. {
  1160. DTRACE(opic, ("timer frequency reporting register - write 0x%x\n", reg));
  1161. opic->timer_frequency = reg;
  1162. }
  1163. /* timer registers: */
  1164. static unsigned
  1165. do_timer_N_current_count_register_read(device *me,
  1166. hw_opic_device *opic,
  1167. int index)
  1168. {
  1169. opic_timer *timer = &opic->timer[index];
  1170. unsigned reg;
  1171. ASSERT(index >= 0 && index < opic->nr_timer_interrupts);
  1172. if (timer->inhibited)
  1173. reg = timer->count; /* stalled value */
  1174. else
  1175. reg = timer->count - device_event_queue_time(me); /* time remaining */
  1176. DTRACE(opic, ("timer %d current count register - read 0x%x\n", index, reg));
  1177. return reg;
  1178. }
  1179. static unsigned
  1180. do_timer_N_base_count_register_read(device *me,
  1181. hw_opic_device *opic,
  1182. int index)
  1183. {
  1184. opic_timer *timer = &opic->timer[index];
  1185. unsigned reg;
  1186. ASSERT(index >= 0 && index < opic->nr_timer_interrupts);
  1187. reg = timer->base_count;
  1188. DTRACE(opic, ("timer %d base count register - read 0x%x\n", index, reg));
  1189. return reg;
  1190. }
  1191. static void
  1192. timer_event(void *data)
  1193. {
  1194. opic_timer *timer = data;
  1195. device *me = timer->me;
  1196. if (timer->inhibited)
  1197. device_error(timer->me, "internal-error - timer event occured when timer %d inhibited",
  1198. timer->nr);
  1199. handle_interrupt(timer->me, timer->opic, timer->interrupt_source, 1);
  1200. timer->timeout_event = device_event_queue_schedule(me, timer->base_count,
  1201. timer_event, timer);
  1202. DTRACE(opic, ("timer %d - interrupt at %ld, next at %d\n",
  1203. timer->nr, (long)device_event_queue_time(me), timer->base_count));
  1204. }
  1205. static void
  1206. do_timer_N_base_count_register_write(device *me,
  1207. hw_opic_device *opic,
  1208. int index,
  1209. unsigned reg)
  1210. {
  1211. opic_timer *timer = &opic->timer[index];
  1212. int inhibit;
  1213. ASSERT(index >= 0 && index < opic->nr_timer_interrupts);
  1214. inhibit = reg & 0x80000000;
  1215. if (timer->inhibited && !inhibit) {
  1216. timer->inhibited = 0;
  1217. if (timer->timeout_event != NULL)
  1218. device_event_queue_deschedule(me, timer->timeout_event);
  1219. timer->count = device_event_queue_time(me) + reg;
  1220. timer->base_count = reg;
  1221. timer->timeout_event = device_event_queue_schedule(me, timer->base_count,
  1222. timer_event, (void*)timer);
  1223. DTRACE(opic, ("timer %d base count register - write 0x%x - timer started\n",
  1224. index, reg));
  1225. }
  1226. else if (!timer->inhibited && inhibit) {
  1227. if (timer->timeout_event != NULL)
  1228. device_event_queue_deschedule(me, timer->timeout_event);
  1229. timer->count = timer->count - device_event_queue_time(me);
  1230. timer->inhibited = 1;
  1231. timer->base_count = reg;
  1232. DTRACE(opic, ("timer %d base count register - write 0x%x - timer stopped\n",
  1233. index, reg));
  1234. }
  1235. else {
  1236. ASSERT((timer->inhibited && inhibit) || (!timer->inhibited && !inhibit));
  1237. DTRACE(opic, ("timer %d base count register - write 0x%x\n", index, reg));
  1238. timer->base_count = reg;
  1239. }
  1240. }
  1241. static unsigned
  1242. do_timer_N_vector_priority_register_read(device *me,
  1243. hw_opic_device *opic,
  1244. int index)
  1245. {
  1246. unsigned reg;
  1247. ASSERT(index >= 0 && index < opic->nr_timer_interrupts);
  1248. reg = read_vector_priority_register(me, opic,
  1249. &opic->timer_interrupt_source[index],
  1250. "timer", index);
  1251. return reg;
  1252. }
  1253. static void
  1254. do_timer_N_vector_priority_register_write(device *me,
  1255. hw_opic_device *opic,
  1256. int index,
  1257. unsigned reg)
  1258. {
  1259. ASSERT(index >= 0 && index < opic->nr_timer_interrupts);
  1260. reg &= ~isu_level_triggered_bit; /* force edge trigger */
  1261. reg |= isu_positive_polarity_bit; /* force rising (positive) edge */
  1262. reg |= isu_multicast_bit; /* force multicast */
  1263. write_vector_priority_register(me, opic,
  1264. &opic->timer_interrupt_source[index],
  1265. reg, "timer", index);
  1266. }
  1267. static unsigned
  1268. do_timer_N_destination_register_read(device *me,
  1269. hw_opic_device *opic,
  1270. int index)
  1271. {
  1272. unsigned reg;
  1273. ASSERT(index >= 0 && index < opic->nr_timer_interrupts);
  1274. reg = read_destination_register(me, opic, &opic->timer_interrupt_source[index],
  1275. "timer", index);
  1276. return reg;
  1277. }
  1278. static void
  1279. do_timer_N_destination_register_write(device *me,
  1280. hw_opic_device *opic,
  1281. int index,
  1282. unsigned reg)
  1283. {
  1284. ASSERT(index >= 0 && index < opic->nr_timer_interrupts);
  1285. write_destination_register(me, opic, &opic->timer_interrupt_source[index],
  1286. reg, "timer", index);
  1287. }
  1288. /* IPI registers */
  1289. static unsigned
  1290. do_ipi_N_vector_priority_register_read(device *me,
  1291. hw_opic_device *opic,
  1292. int index)
  1293. {
  1294. unsigned reg;
  1295. ASSERT(index >= 0 && index < opic->nr_interprocessor_interrupts);
  1296. reg = read_vector_priority_register(me, opic,
  1297. &opic->interprocessor_interrupt_source[index],
  1298. "ipi", index);
  1299. return reg;
  1300. }
  1301. static void
  1302. do_ipi_N_vector_priority_register_write(device *me,
  1303. hw_opic_device *opic,
  1304. int index,
  1305. unsigned reg)
  1306. {
  1307. ASSERT(index >= 0 && index < opic->nr_interprocessor_interrupts);
  1308. reg &= ~isu_level_triggered_bit; /* force edge trigger */
  1309. reg |= isu_positive_polarity_bit; /* force rising (positive) edge */
  1310. reg |= isu_multicast_bit; /* force a multicast source */
  1311. write_vector_priority_register(me, opic,
  1312. &opic->interprocessor_interrupt_source[index],
  1313. reg, "ipi", index);
  1314. }
  1315. static void
  1316. do_ipi_N_dispatch_register_write(device *me,
  1317. hw_opic_device *opic,
  1318. int index,
  1319. unsigned reg)
  1320. {
  1321. opic_interrupt_source *source = &opic->interprocessor_interrupt_source[index];
  1322. ASSERT(index >= 0 && index < opic->nr_interprocessor_interrupts);
  1323. DTRACE(opic, ("ipi %d interrupt dispatch register - write 0x%x\n", index, reg));
  1324. source->destination = reg;
  1325. handle_interrupt(me, opic, source, 1);
  1326. }
  1327. /* vendor and other global registers */
  1328. static unsigned
  1329. do_vendor_identification_register_read(device *me,
  1330. hw_opic_device *opic)
  1331. {
  1332. unsigned reg;
  1333. reg = opic->vendor_identification;
  1334. DTRACE(opic, ("vendor identification register - read 0x%x\n", reg));
  1335. return reg;
  1336. }
  1337. static unsigned
  1338. do_feature_reporting_register_N_read(device *me,
  1339. hw_opic_device *opic,
  1340. int index)
  1341. {
  1342. unsigned reg = 0;
  1343. ASSERT(index == 0);
  1344. switch (index) {
  1345. case 0:
  1346. reg |= (opic->nr_external_interrupts << 16);
  1347. reg |= (opic->nr_interrupt_destinations << 8);
  1348. reg |= (2/*version 1.2*/);
  1349. break;
  1350. }
  1351. DTRACE(opic, ("feature reporting register %d - read 0x%x\n", index, reg));
  1352. return reg;
  1353. }
  1354. static unsigned
  1355. do_global_configuration_register_N_read(device *me,
  1356. hw_opic_device *opic,
  1357. int index)
  1358. {
  1359. unsigned reg = 0;
  1360. ASSERT(index == 0);
  1361. switch (index) {
  1362. case 0:
  1363. reg |= gcr0_8259_bit; /* hardwire 8259 disabled */
  1364. break;
  1365. }
  1366. DTRACE(opic, ("global configuration register %d - read 0x%x\n", index, reg));
  1367. return reg;
  1368. }
  1369. static void
  1370. do_global_configuration_register_N_write(device *me,
  1371. hw_opic_device *opic,
  1372. int index,
  1373. unsigned reg)
  1374. {
  1375. ASSERT(index == 0);
  1376. if (reg & gcr0_reset_bit) {
  1377. DTRACE(opic, ("global configuration register %d - write 0x%x - reseting opic\n", index, reg));
  1378. hw_opic_init_data(me);
  1379. }
  1380. if (!(reg & gcr0_8259_bit)) {
  1381. DTRACE(opic, ("global configuration register %d - write 0x%x - ignoring 8259 enable\n", index, reg));
  1382. }
  1383. }
  1384. /* register read-write */
  1385. static unsigned
  1386. hw_opic_io_read_buffer(device *me,
  1387. void *dest,
  1388. int space,
  1389. unsigned_word addr,
  1390. unsigned nr_bytes,
  1391. cpu *processor,
  1392. unsigned_word cia)
  1393. {
  1394. hw_opic_device *opic = (hw_opic_device*)device_data(me);
  1395. opic_register type;
  1396. int index;
  1397. decode_opic_address(me, opic, space, addr, nr_bytes, &type, &index);
  1398. if (type == invalid_opic_register) {
  1399. device_error(me, "invalid opic read access to %d:0x%lx (%d bytes)",
  1400. space, (unsigned long)addr, nr_bytes);
  1401. }
  1402. else {
  1403. unsigned reg;
  1404. switch (type) {
  1405. case processor_init_register:
  1406. reg = do_processor_init_register_read(me, opic);
  1407. break;
  1408. case interrupt_source_N_vector_priority_register:
  1409. reg = do_interrupt_source_N_vector_priority_register_read(me, opic, index);
  1410. break;
  1411. case interrupt_source_N_destination_register:
  1412. reg = do_interrupt_source_N_destination_register_read(me, opic, index);
  1413. break;
  1414. case interrupt_acknowledge_register_N:
  1415. reg = do_interrupt_acknowledge_register_N_read(me, opic, index);
  1416. break;
  1417. case spurious_vector_register:
  1418. reg = do_spurious_vector_register_read(me, opic);
  1419. break;
  1420. case current_task_priority_register_N:
  1421. reg = do_current_task_priority_register_N_read(me, opic, index);
  1422. break;
  1423. case timer_frequency_reporting_register:
  1424. reg = do_timer_frequency_reporting_register_read(me, opic);
  1425. break;
  1426. case timer_N_current_count_register:
  1427. reg = do_timer_N_current_count_register_read(me, opic, index);
  1428. break;
  1429. case timer_N_base_count_register:
  1430. reg = do_timer_N_base_count_register_read(me, opic, index);
  1431. break;
  1432. case timer_N_vector_priority_register:
  1433. reg = do_timer_N_vector_priority_register_read(me, opic, index);
  1434. break;
  1435. case timer_N_destination_register:
  1436. reg = do_timer_N_destination_register_read(me, opic, index);
  1437. break;
  1438. case ipi_N_vector_priority_register:
  1439. reg = do_ipi_N_vector_priority_register_read(me, opic, index);
  1440. break;
  1441. case feature_reporting_register_N:
  1442. reg = do_feature_reporting_register_N_read(me, opic, index);
  1443. break;
  1444. case global_configuration_register_N:
  1445. reg = do_global_configuration_register_N_read(me, opic, index);
  1446. break;
  1447. case vendor_identification_register:
  1448. reg = do_vendor_identification_register_read(me, opic);
  1449. break;
  1450. default:
  1451. reg = 0;
  1452. device_error(me, "unimplemented read of register %s[%d]",
  1453. opic_register_name(type), index);
  1454. }
  1455. *(unsigned_4*)dest = H2LE_4(reg);
  1456. }
  1457. return nr_bytes;
  1458. }
  1459. static unsigned
  1460. hw_opic_io_write_buffer(device *me,
  1461. const void *source,
  1462. int space,
  1463. unsigned_word addr,
  1464. unsigned nr_bytes,
  1465. cpu *processor,
  1466. unsigned_word cia)
  1467. {
  1468. hw_opic_device *opic = (hw_opic_device*)device_data(me);
  1469. opic_register type;
  1470. int index;
  1471. decode_opic_address(me, opic, space, addr, nr_bytes, &type, &index);
  1472. if (type == invalid_opic_register) {
  1473. device_error(me, "invalid opic write access to %d:0x%lx (%d bytes)",
  1474. space, (unsigned long)addr, nr_bytes);
  1475. }
  1476. else {
  1477. unsigned reg = LE2H_4(*(unsigned_4*)source);
  1478. switch (type) {
  1479. case processor_init_register:
  1480. do_processor_init_register_write(me, opic, reg);
  1481. break;
  1482. case interrupt_source_N_vector_priority_register:
  1483. do_interrupt_source_N_vector_priority_register_write(me, opic, index, reg);
  1484. break;
  1485. case interrupt_source_N_destination_register:
  1486. do_interrupt_source_N_destination_register_write(me, opic, index, reg);
  1487. break;
  1488. case end_of_interrupt_register_N:
  1489. do_end_of_interrupt_register_N_write(me, opic, index, reg);
  1490. break;
  1491. case spurious_vector_register:
  1492. do_spurious_vector_register_write(me, opic, reg);
  1493. break;
  1494. case current_task_priority_register_N:
  1495. do_current_task_priority_register_N_write(me, opic, index, reg);
  1496. break;
  1497. case timer_frequency_reporting_register:
  1498. do_timer_frequency_reporting_register_write(me, opic, reg);
  1499. break;
  1500. case timer_N_base_count_register:
  1501. do_timer_N_base_count_register_write(me, opic, index, reg);
  1502. break;
  1503. case timer_N_vector_priority_register:
  1504. do_timer_N_vector_priority_register_write(me, opic, index, reg);
  1505. break;
  1506. case timer_N_destination_register:
  1507. do_timer_N_destination_register_write(me, opic, index, reg);
  1508. break;
  1509. case ipi_N_dispatch_register:
  1510. do_ipi_N_dispatch_register_write(me, opic, index, reg);
  1511. break;
  1512. case ipi_N_vector_priority_register:
  1513. do_ipi_N_vector_priority_register_write(me, opic, index, reg);
  1514. break;
  1515. case global_configuration_register_N:
  1516. do_global_configuration_register_N_write(me, opic, index, reg);
  1517. break;
  1518. default:
  1519. device_error(me, "unimplemented write to register %s[%d]",
  1520. opic_register_name(type), index);
  1521. }
  1522. }
  1523. return nr_bytes;
  1524. }
  1525. static void
  1526. hw_opic_interrupt_event(device *me,
  1527. int my_port,
  1528. device *source,
  1529. int source_port,
  1530. int level,
  1531. cpu *processor,
  1532. unsigned_word cia)
  1533. {
  1534. hw_opic_device *opic = (hw_opic_device*)device_data(me);
  1535. int isb;
  1536. int src_nr = 0;
  1537. /* find the corresponding internal input port */
  1538. for (isb = 0; isb < opic->nr_isu_blocks; isb++) {
  1539. if (my_port >= opic->isu_block[isb].int_number
  1540. && my_port < opic->isu_block[isb].int_number + opic->isu_block[isb].range) {
  1541. src_nr += my_port - opic->isu_block[isb].int_number;
  1542. break;
  1543. }
  1544. else
  1545. src_nr += opic->isu_block[isb].range;
  1546. }
  1547. if (isb == opic->nr_isu_blocks)
  1548. device_error(me, "interrupt %d out of range", my_port);
  1549. DTRACE(opic, ("external-interrupt %d, internal %d, level %d\n",
  1550. my_port, src_nr, level));
  1551. /* pass it on */
  1552. ASSERT(src_nr >= 0 && src_nr < opic->nr_external_interrupts);
  1553. handle_interrupt(me, opic, &opic->external_interrupt_source[src_nr], level);
  1554. }
  1555. static const device_interrupt_port_descriptor hw_opic_interrupt_ports[] = {
  1556. { "irq", 0, max_nr_interrupt_sources, input_port, },
  1557. { "intr", 0, max_nr_interrupt_destinations, output_port, },
  1558. { "init", max_nr_interrupt_destinations, max_nr_interrupt_destinations, output_port, },
  1559. { NULL }
  1560. };
  1561. static device_callbacks const hw_opic_callbacks = {
  1562. { generic_device_init_address,
  1563. hw_opic_init_data },
  1564. { NULL, }, /* address */
  1565. { hw_opic_io_read_buffer,
  1566. hw_opic_io_write_buffer }, /* IO */
  1567. { NULL, }, /* DMA */
  1568. { hw_opic_interrupt_event, NULL, hw_opic_interrupt_ports }, /* interrupt */
  1569. { NULL, }, /* unit */
  1570. NULL, /* instance */
  1571. };
  1572. static void *
  1573. hw_opic_create(const char *name,
  1574. const device_unit *unit_address,
  1575. const char *args)
  1576. {
  1577. hw_opic_device *opic = ZALLOC(hw_opic_device);
  1578. return opic;
  1579. }
  1580. const device_descriptor hw_opic_device_descriptor[] = {
  1581. { "opic", hw_opic_create, &hw_opic_callbacks },
  1582. { NULL },
  1583. };
  1584. #endif /* _HW_OPIC_C_ */