1 /* $NetBSD: tc_bus_mem.c,v 1.29 2009/03/14 15:36:00 dsl Exp $ */
4 * Copyright (c) 1996 Carnegie-Mellon University.
7 * Author: Chris G. Demetriou
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19 * Carnegie Mellon requests users of this software to return to
21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
31 * Common TurboChannel Chipset "bus memory" functions.
34 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
36 __KERNEL_RCSID(0, "$NetBSD: tc_bus_mem.c,v 1.29 2009/03/14 15:36:00 dsl Exp $");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/syslog.h>
42 #include <sys/device.h>
44 #include <uvm/uvm_extern.h>
46 #include <machine/bus.h>
47 #include <dev/tc/tcvar.h>
49 #define __C(A,B) __CONCAT(A,B)
51 /* mapping/unmapping */
52 int tc_mem_map(void *, bus_addr_t
, bus_size_t
, int,
53 bus_space_handle_t
*, int);
54 void tc_mem_unmap(void *, bus_space_handle_t
, bus_size_t
, int);
55 int tc_mem_subregion(void *, bus_space_handle_t
, bus_size_t
,
56 bus_size_t
, bus_space_handle_t
*);
58 int tc_mem_translate(void *, bus_addr_t
, bus_size_t
,
59 int, struct alpha_bus_space_translation
*);
60 int tc_mem_get_window(void *, int,
61 struct alpha_bus_space_translation
*);
63 /* allocation/deallocation */
64 int tc_mem_alloc(void *, bus_addr_t
, bus_addr_t
, bus_size_t
,
65 bus_size_t
, bus_addr_t
, int, bus_addr_t
*,
66 bus_space_handle_t
*);
67 void tc_mem_free(void *, bus_space_handle_t
, bus_size_t
);
69 /* get kernel virtual address */
70 void * tc_mem_vaddr(void *, bus_space_handle_t
);
73 paddr_t
tc_mem_mmap(void *, bus_addr_t
, off_t
, int, int);
76 inline void tc_mem_barrier(void *, bus_space_handle_t
,
77 bus_size_t
, bus_size_t
, int);
80 inline u_int8_t
tc_mem_read_1(void *, bus_space_handle_t
, bus_size_t
);
81 inline u_int16_t
tc_mem_read_2(void *, bus_space_handle_t
, bus_size_t
);
82 inline u_int32_t
tc_mem_read_4(void *, bus_space_handle_t
, bus_size_t
);
83 inline u_int64_t
tc_mem_read_8(void *, bus_space_handle_t
, bus_size_t
);
86 void tc_mem_read_multi_1(void *, bus_space_handle_t
,
87 bus_size_t
, u_int8_t
*, bus_size_t
);
88 void tc_mem_read_multi_2(void *, bus_space_handle_t
,
89 bus_size_t
, u_int16_t
*, bus_size_t
);
90 void tc_mem_read_multi_4(void *, bus_space_handle_t
,
91 bus_size_t
, u_int32_t
*, bus_size_t
);
92 void tc_mem_read_multi_8(void *, bus_space_handle_t
,
93 bus_size_t
, u_int64_t
*, bus_size_t
);
96 void tc_mem_read_region_1(void *, bus_space_handle_t
,
97 bus_size_t
, u_int8_t
*, bus_size_t
);
98 void tc_mem_read_region_2(void *, bus_space_handle_t
,
99 bus_size_t
, u_int16_t
*, bus_size_t
);
100 void tc_mem_read_region_4(void *, bus_space_handle_t
,
101 bus_size_t
, u_int32_t
*, bus_size_t
);
102 void tc_mem_read_region_8(void *, bus_space_handle_t
,
103 bus_size_t
, u_int64_t
*, bus_size_t
);
106 inline void tc_mem_write_1(void *, bus_space_handle_t
, bus_size_t
,
108 inline void tc_mem_write_2(void *, bus_space_handle_t
, bus_size_t
,
110 inline void tc_mem_write_4(void *, bus_space_handle_t
, bus_size_t
,
112 inline void tc_mem_write_8(void *, bus_space_handle_t
, bus_size_t
,
116 void tc_mem_write_multi_1(void *, bus_space_handle_t
,
117 bus_size_t
, const u_int8_t
*, bus_size_t
);
118 void tc_mem_write_multi_2(void *, bus_space_handle_t
,
119 bus_size_t
, const u_int16_t
*, bus_size_t
);
120 void tc_mem_write_multi_4(void *, bus_space_handle_t
,
121 bus_size_t
, const u_int32_t
*, bus_size_t
);
122 void tc_mem_write_multi_8(void *, bus_space_handle_t
,
123 bus_size_t
, const u_int64_t
*, bus_size_t
);
126 void tc_mem_write_region_1(void *, bus_space_handle_t
,
127 bus_size_t
, const u_int8_t
*, bus_size_t
);
128 void tc_mem_write_region_2(void *, bus_space_handle_t
,
129 bus_size_t
, const u_int16_t
*, bus_size_t
);
130 void tc_mem_write_region_4(void *, bus_space_handle_t
,
131 bus_size_t
, const u_int32_t
*, bus_size_t
);
132 void tc_mem_write_region_8(void *, bus_space_handle_t
,
133 bus_size_t
, const u_int64_t
*, bus_size_t
);
136 void tc_mem_set_multi_1(void *, bus_space_handle_t
,
137 bus_size_t
, u_int8_t
, bus_size_t
);
138 void tc_mem_set_multi_2(void *, bus_space_handle_t
,
139 bus_size_t
, u_int16_t
, bus_size_t
);
140 void tc_mem_set_multi_4(void *, bus_space_handle_t
,
141 bus_size_t
, u_int32_t
, bus_size_t
);
142 void tc_mem_set_multi_8(void *, bus_space_handle_t
,
143 bus_size_t
, u_int64_t
, bus_size_t
);
146 void tc_mem_set_region_1(void *, bus_space_handle_t
,
147 bus_size_t
, u_int8_t
, bus_size_t
);
148 void tc_mem_set_region_2(void *, bus_space_handle_t
,
149 bus_size_t
, u_int16_t
, bus_size_t
);
150 void tc_mem_set_region_4(void *, bus_space_handle_t
,
151 bus_size_t
, u_int32_t
, bus_size_t
);
152 void tc_mem_set_region_8(void *, bus_space_handle_t
,
153 bus_size_t
, u_int64_t
, bus_size_t
);
156 void tc_mem_copy_region_1(void *, bus_space_handle_t
,
157 bus_size_t
, bus_space_handle_t
, bus_size_t
, bus_size_t
);
158 void tc_mem_copy_region_2(void *, bus_space_handle_t
,
159 bus_size_t
, bus_space_handle_t
, bus_size_t
, bus_size_t
);
160 void tc_mem_copy_region_4(void *, bus_space_handle_t
,
161 bus_size_t
, bus_space_handle_t
, bus_size_t
, bus_size_t
);
162 void tc_mem_copy_region_8(void *, bus_space_handle_t
,
163 bus_size_t
, bus_space_handle_t
, bus_size_t
, bus_size_t
);
165 static struct alpha_bus_space tc_mem_space
= {
169 /* mapping/unmapping */
177 /* allocation/deallocation */
181 /* get kernel virtual address */
203 tc_mem_read_region_1
,
204 tc_mem_read_region_2
,
205 tc_mem_read_region_4
,
206 tc_mem_read_region_8
,
215 tc_mem_write_multi_1
,
216 tc_mem_write_multi_2
,
217 tc_mem_write_multi_4
,
218 tc_mem_write_multi_8
,
221 tc_mem_write_region_1
,
222 tc_mem_write_region_2
,
223 tc_mem_write_region_4
,
224 tc_mem_write_region_8
,
239 tc_mem_copy_region_1
,
240 tc_mem_copy_region_2
,
241 tc_mem_copy_region_4
,
242 tc_mem_copy_region_8
,
246 tc_bus_mem_init(void *memv
)
248 bus_space_tag_t h
= &tc_mem_space
;
250 h
->abs_cookie
= memv
;
256 tc_mem_translate(void *v
, bus_addr_t memaddr
, bus_size_t memlen
, int flags
, struct alpha_bus_space_translation
*abst
)
264 tc_mem_get_window(void *v
, int window
, struct alpha_bus_space_translation
*abst
)
272 tc_mem_map(void *v
, bus_addr_t memaddr
, bus_size_t memsize
, int flags
, bus_space_handle_t
*memhp
, int acct
)
274 int cacheable
= flags
& BUS_SPACE_MAP_CACHEABLE
;
275 int linear
= flags
& BUS_SPACE_MAP_LINEAR
;
277 /* Requests for linear uncacheable space can't be satisfied. */
278 if (linear
&& !cacheable
)
282 panic("tc_mem_map needs 8 byte alignment");
284 *memhp
= ALPHA_PHYS_TO_K0SEG(memaddr
);
286 *memhp
= ALPHA_PHYS_TO_K0SEG(TC_DENSE_TO_SPARSE(memaddr
));
292 tc_mem_unmap(void *v
, bus_space_handle_t memh
, bus_size_t memsize
, int acct
)
295 /* XXX XX XXX nothing to do. */
299 tc_mem_subregion(void *v
, bus_space_handle_t memh
, bus_size_t offset
, bus_size_t size
, bus_space_handle_t
*nmemh
)
302 /* Disallow subregioning that would make the handle unaligned. */
303 if ((offset
& 0x7) != 0)
306 if ((memh
& TC_SPACE_SPARSE
) != 0)
307 *nmemh
= memh
+ (offset
<< 1);
309 *nmemh
= memh
+ offset
;
315 tc_mem_alloc(void *v
, bus_addr_t rstart
, bus_addr_t rend
, bus_size_t size
, bus_size_t align
, bus_size_t boundary
, int flags
, bus_addr_t
*addrp
, bus_space_handle_t
*bshp
)
318 /* XXX XXX XXX XXX XXX XXX */
319 panic("tc_mem_alloc unimplemented");
323 tc_mem_free(void *v
, bus_space_handle_t bsh
, bus_size_t size
)
326 /* XXX XXX XXX XXX XXX XXX */
327 panic("tc_mem_free unimplemented");
331 tc_mem_vaddr(void *v
, bus_space_handle_t bsh
)
334 if ((bsh
& TC_SPACE_SPARSE
) != 0) {
336 * tc_mem_map() catches linear && !cacheable,
337 * so we shouldn't come here
339 panic("tc_mem_vaddr");
342 return ((void *)bsh
);
346 tc_mem_mmap(void *v
, bus_addr_t addr
, off_t off
, int prot
, int flags
)
348 int linear
= flags
& BUS_SPACE_MAP_LINEAR
;
354 rv
= TC_DENSE_TO_SPARSE(addr
+ off
);
356 return (alpha_btop(rv
));
360 tc_mem_barrier(void *v
, bus_space_handle_t h
, bus_size_t o
, bus_size_t l
, int f
)
363 if ((f
& BUS_SPACE_BARRIER_READ
) != 0)
365 else if ((f
& BUS_SPACE_BARRIER_WRITE
) != 0)
370 tc_mem_read_1(void *v
, bus_space_handle_t memh
, bus_size_t off
)
372 volatile u_int8_t
*p
;
374 alpha_mb(); /* XXX XXX XXX */
376 if ((memh
& TC_SPACE_SPARSE
) != 0)
377 panic("tc_mem_read_1 not implemented for sparse space");
379 p
= (u_int8_t
*)(memh
+ off
);
384 tc_mem_read_2(void *v
, bus_space_handle_t memh
, bus_size_t off
)
386 volatile u_int16_t
*p
;
388 alpha_mb(); /* XXX XXX XXX */
390 if ((memh
& TC_SPACE_SPARSE
) != 0)
391 panic("tc_mem_read_2 not implemented for sparse space");
393 p
= (u_int16_t
*)(memh
+ off
);
398 tc_mem_read_4(void *v
, bus_space_handle_t memh
, bus_size_t off
)
400 volatile u_int32_t
*p
;
402 alpha_mb(); /* XXX XXX XXX */
404 if ((memh
& TC_SPACE_SPARSE
) != 0)
405 /* Nothing special to do for 4-byte sparse space accesses */
406 p
= (u_int32_t
*)(memh
+ (off
<< 1));
408 p
= (u_int32_t
*)(memh
+ off
);
413 tc_mem_read_8(void *v
, bus_space_handle_t memh
, bus_size_t off
)
415 volatile u_int64_t
*p
;
417 alpha_mb(); /* XXX XXX XXX */
419 if ((memh
& TC_SPACE_SPARSE
) != 0)
420 panic("tc_mem_read_8 not implemented for sparse space");
422 p
= (u_int64_t
*)(memh
+ off
);
426 #define tc_mem_read_multi_N(BYTES,TYPE) \
428 __C(tc_mem_read_multi_,BYTES)(v, h, o, a, c) \
430 bus_space_handle_t h; \
436 tc_mem_barrier(v, h, o, sizeof *a, BUS_SPACE_BARRIER_READ); \
437 *a++ = __C(tc_mem_read_,BYTES)(v, h, o); \
440 tc_mem_read_multi_N(1,u_int8_t
)
441 tc_mem_read_multi_N(2,u_int16_t
)
442 tc_mem_read_multi_N(4,u_int32_t
)
443 tc_mem_read_multi_N(8,u_int64_t
)
445 #define tc_mem_read_region_N(BYTES,TYPE) \
447 __C(tc_mem_read_region_,BYTES)(v, h, o, a, c) \
449 bus_space_handle_t h; \
455 *a++ = __C(tc_mem_read_,BYTES)(v, h, o); \
459 tc_mem_read_region_N(1,u_int8_t
)
460 tc_mem_read_region_N(2,u_int16_t
)
461 tc_mem_read_region_N(4,u_int32_t
)
462 tc_mem_read_region_N(8,u_int64_t
)
465 tc_mem_write_1(void *v
, bus_space_handle_t memh
, bus_size_t off
, u_int8_t val
)
468 if ((memh
& TC_SPACE_SPARSE
) != 0) {
469 volatile u_int64_t
*p
, vl
;
470 u_int64_t shift
, msk
;
475 p
= (u_int64_t
*)(memh
+ (off
<< 1));
477 msk
= ~(0x1 << shift
) & 0xf;
478 vl
= (msk
<< 32) | (((u_int64_t
)val
) << (shift
* 8));
482 volatile u_int8_t
*p
;
484 p
= (u_int8_t
*)(memh
+ off
);
487 alpha_mb(); /* XXX XXX XXX */
491 tc_mem_write_2(void *v
, bus_space_handle_t memh
, bus_size_t off
, u_int16_t val
)
494 if ((memh
& TC_SPACE_SPARSE
) != 0) {
495 volatile u_int64_t
*p
, vl
;
496 u_int64_t shift
, msk
;
501 p
= (u_int64_t
*)(memh
+ (off
<< 1));
503 msk
= ~(0x3 << shift
) & 0xf;
504 vl
= (msk
<< 32) | (((u_int64_t
)val
) << (shift
* 8));
508 volatile u_int16_t
*p
;
510 p
= (u_int16_t
*)(memh
+ off
);
513 alpha_mb(); /* XXX XXX XXX */
517 tc_mem_write_4(void *v
, bus_space_handle_t memh
, bus_size_t off
, u_int32_t val
)
519 volatile u_int32_t
*p
;
521 if ((memh
& TC_SPACE_SPARSE
) != 0)
522 /* Nothing special to do for 4-byte sparse space accesses */
523 p
= (u_int32_t
*)(memh
+ (off
<< 1));
525 p
= (u_int32_t
*)(memh
+ off
);
527 alpha_mb(); /* XXX XXX XXX */
531 tc_mem_write_8(void *v
, bus_space_handle_t memh
, bus_size_t off
, u_int64_t val
)
533 volatile u_int64_t
*p
;
535 if ((memh
& TC_SPACE_SPARSE
) != 0)
536 panic("tc_mem_read_8 not implemented for sparse space");
538 p
= (u_int64_t
*)(memh
+ off
);
540 alpha_mb(); /* XXX XXX XXX */
543 #define tc_mem_write_multi_N(BYTES,TYPE) \
545 __C(tc_mem_write_multi_,BYTES)(v, h, o, a, c) \
547 bus_space_handle_t h; \
553 __C(tc_mem_write_,BYTES)(v, h, o, *a++); \
554 tc_mem_barrier(v, h, o, sizeof *a, BUS_SPACE_BARRIER_WRITE); \
557 tc_mem_write_multi_N(1,u_int8_t
)
558 tc_mem_write_multi_N(2,u_int16_t
)
559 tc_mem_write_multi_N(4,u_int32_t
)
560 tc_mem_write_multi_N(8,u_int64_t
)
562 #define tc_mem_write_region_N(BYTES,TYPE) \
564 __C(tc_mem_write_region_,BYTES)(v, h, o, a, c) \
566 bus_space_handle_t h; \
572 __C(tc_mem_write_,BYTES)(v, h, o, *a++); \
576 tc_mem_write_region_N(1,u_int8_t
)
577 tc_mem_write_region_N(2,u_int16_t
)
578 tc_mem_write_region_N(4,u_int32_t
)
579 tc_mem_write_region_N(8,u_int64_t
)
581 #define tc_mem_set_multi_N(BYTES,TYPE) \
583 __C(tc_mem_set_multi_,BYTES)(v, h, o, val, c) \
585 bus_space_handle_t h; \
591 __C(tc_mem_write_,BYTES)(v, h, o, val); \
592 tc_mem_barrier(v, h, o, sizeof val, BUS_SPACE_BARRIER_WRITE); \
595 tc_mem_set_multi_N(1,u_int8_t
)
596 tc_mem_set_multi_N(2,u_int16_t
)
597 tc_mem_set_multi_N(4,u_int32_t
)
598 tc_mem_set_multi_N(8,u_int64_t
)
600 #define tc_mem_set_region_N(BYTES,TYPE) \
602 __C(tc_mem_set_region_,BYTES)(v, h, o, val, c) \
604 bus_space_handle_t h; \
610 __C(tc_mem_write_,BYTES)(v, h, o, val); \
614 tc_mem_set_region_N(1,u_int8_t
)
615 tc_mem_set_region_N(2,u_int16_t
)
616 tc_mem_set_region_N(4,u_int32_t
)
617 tc_mem_set_region_N(8,u_int64_t
)
619 #define tc_mem_copy_region_N(BYTES) \
621 __C(tc_mem_copy_region_,BYTES)(v, h1, o1, h2, o2, c) \
623 bus_space_handle_t h1, h2; \
624 bus_size_t o1, o2, c; \
628 if ((h1 & TC_SPACE_SPARSE) != 0 && \
629 (h2 & TC_SPACE_SPARSE) != 0) { \
630 memmove((void *)(h2 + o2), (void *)(h1 + o1), c * BYTES); \
634 if (h1 + o1 >= h2 + o2) \
635 /* src after dest: copy forward */ \
636 for (o = 0; c > 0; c--, o += BYTES) \
637 __C(tc_mem_write_,BYTES)(v, h2, o2 + o, \
638 __C(tc_mem_read_,BYTES)(v, h1, o1 + o)); \
640 /* dest after src: copy backwards */ \
641 for (o = (c - 1) * BYTES; c > 0; c--, o -= BYTES) \
642 __C(tc_mem_write_,BYTES)(v, h2, o2 + o, \
643 __C(tc_mem_read_,BYTES)(v, h1, o1 + o)); \
645 tc_mem_copy_region_N(1)
646 tc_mem_copy_region_N(2)
647 tc_mem_copy_region_N(4)
648 tc_mem_copy_region_N(8)