Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/*	$NetBSD: bus_space.c,v 1.39 2022/03/10 00:14:16 riastradh Exp $	*/

/*-
 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
 * NASA Ames Research Center.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#define _POWERPC_BUS_SPACE_PRIVATE

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.39 2022/03/10 00:14:16 riastradh Exp $");

#ifdef _KERNEL_OPT
#include "opt_ppcarch.h"
#endif

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/endian.h>
#include <sys/extent.h>
#include <sys/bus.h>

#include <uvm/uvm.h>

#if defined (PPC_OEA) || defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
#include <powerpc/spr.h>
#include <powerpc/oea/bat.h>
#include <powerpc/oea/cpufeat.h>
#include <powerpc/oea/pte.h>
#include <powerpc/oea/spr.h>
#include <powerpc/oea/sr_601.h>
#endif

/* read_N */
u_int8_t bsr1(bus_space_tag_t, bus_space_handle_t, bus_size_t);
u_int16_t bsr2(bus_space_tag_t, bus_space_handle_t, bus_size_t);
u_int32_t bsr4(bus_space_tag_t, bus_space_handle_t, bus_size_t);
u_int64_t bsr8(bus_space_tag_t, bus_space_handle_t, bus_size_t);

/* write_N */
void bsw1(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int8_t);
void bsw2(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t);
void bsw4(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
void bsw8(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t);

static const struct powerpc_bus_space_scalar scalar_ops = {
	bsr1, bsr2, bsr4, bsr8,
	bsw1, bsw2, bsw4, bsw8
};

/* read_N byte reverse */
u_int16_t bsr2rb(bus_space_tag_t, bus_space_handle_t, bus_size_t);
u_int32_t bsr4rb(bus_space_tag_t, bus_space_handle_t, bus_size_t);
u_int64_t bsr8rb(bus_space_tag_t, bus_space_handle_t, bus_size_t);

/* write_N byte reverse */
void bsw2rb(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t);
void bsw4rb(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
void bsw8rb(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t);

static const struct powerpc_bus_space_scalar scalar_rb_ops = {
	bsr1, bsr2rb, bsr4rb, bsr8rb,
	bsw1, bsw2rb, bsw4rb, bsw8rb
};

/* read_multi_N */
void bsrm1(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int8_t *,
	size_t);
void bsrm2(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t *,
	size_t);
void bsrm4(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t *,
	size_t);
void bsrm8(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t *,
	size_t);

/* write_multi_N */
void bswm1(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int8_t *, size_t);
void bswm2(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int16_t *, size_t);
void bswm4(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int32_t *, size_t);
void bswm8(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int64_t *, size_t);

static const struct powerpc_bus_space_group multi_ops = {
	bsrm1, bsrm2, bsrm4, bsrm8,
	bswm1, bswm2, bswm4, bswm8
};

/* read_multi_N byte reversed */
void bsrm2rb(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t *,
	size_t);
void bsrm4rb(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t *,
	size_t);
void bsrm8rb(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t *,
	size_t);

/* write_multi_N byte reversed */
void bswm2rb(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int16_t *, size_t);
void bswm4rb(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int32_t *, size_t);
void bswm8rb(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int64_t *, size_t);

static const struct powerpc_bus_space_group multi_rb_ops = {
	bsrm1, bsrm2rb, bsrm4rb, bsrm8rb,
	bswm1, bswm2rb, bswm4rb, bswm8rb
};

/* read_region_N */
void bsrr1(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int8_t *,
	size_t);
void bsrr2(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t *,
	size_t);
void bsrr4(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t *,
	size_t);
void bsrr8(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t *,
	size_t);

/* write_region_N */
void bswr1(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int8_t *, size_t);
void bswr2(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int16_t *, size_t);
void bswr4(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int32_t *, size_t);
void bswr8(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int64_t *, size_t);

static const struct powerpc_bus_space_group region_ops = {
	bsrr1, bsrr2, bsrr4, bsrr8,
	bswr1, bswr2, bswr4, bswr8
};

void bsrr2rb(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t *,
	size_t);
void bsrr4rb(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t *,
	size_t);
void bsrr8rb(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t *,
	size_t);

void bswr2rb(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int16_t *, size_t);
void bswr4rb(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int32_t *, size_t);
void bswr8rb(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int64_t *, size_t);

static const struct powerpc_bus_space_group region_rb_ops = {
	bsrr1, bsrr2rb, bsrr4rb, bsrr8rb,
	bswr1, bswr2rb, bswr4rb, bswr8rb
};

/* set_region_n */
void bssr1(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int8_t,
	size_t);
void bssr2(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t,
	size_t);
void bssr4(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t,
	size_t);
void bssr8(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t,
	size_t);

static const struct powerpc_bus_space_set set_ops = {
	bssr1, bssr2, bssr4, bssr8,
};

void bssr2rb(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t,
	size_t);
void bssr4rb(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t,
	size_t);
void bssr8rb(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t,
	size_t);

static const struct powerpc_bus_space_set set_rb_ops = {
	bssr1, bssr2rb, bssr4rb, bssr8rb,
};

/* copy_region_N */
void bscr1(bus_space_tag_t, bus_space_handle_t,
    bus_size_t, bus_space_handle_t, bus_size_t, size_t);
void bscr2(bus_space_tag_t, bus_space_handle_t,
    bus_size_t, bus_space_handle_t, bus_size_t, size_t);
void bscr4(bus_space_tag_t, bus_space_handle_t,
    bus_size_t, bus_space_handle_t, bus_size_t, size_t);
void bscr8(bus_space_tag_t, bus_space_handle_t,
    bus_size_t, bus_space_handle_t, bus_size_t, size_t);

static const struct powerpc_bus_space_copy copy_ops = {
	bscr1, bscr2, bscr4, bscr8
};

/*
 * Strided versions
 */
/* read_N */
u_int8_t bsr1_s(bus_space_tag_t, bus_space_handle_t, bus_size_t);
u_int16_t bsr2_s(bus_space_tag_t, bus_space_handle_t, bus_size_t);
u_int32_t bsr4_s(bus_space_tag_t, bus_space_handle_t, bus_size_t);
u_int64_t bsr8_s(bus_space_tag_t, bus_space_handle_t, bus_size_t);

/* write_N */
void bsw1_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int8_t);
void bsw2_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t);
void bsw4_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
void bsw8_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t);

static const struct powerpc_bus_space_scalar scalar_strided_ops = {
	bsr1_s, bsr2_s, bsr4_s, bsr8_s,
	bsw1_s, bsw2_s, bsw4_s, bsw8_s
};

/* read_N */
u_int16_t bsr2rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t);
u_int32_t bsr4rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t);
u_int64_t bsr8rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t);

/* write_N */
void bsw2rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t);
void bsw4rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t);
void bsw8rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t);

static const struct powerpc_bus_space_scalar scalar_rb_strided_ops = {
	bsr1_s, bsr2rb_s, bsr4rb_s, bsr8rb_s,
	bsw1_s, bsw2rb_s, bsw4rb_s, bsw8rb_s
};

/* read_multi_N */
void bsrm1_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int8_t *,
	size_t);
void bsrm2_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t *,
	size_t);
void bsrm4_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t *,
	size_t);
void bsrm8_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t *,
	size_t);

/* write_multi_N */
void bswm1_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int8_t *, size_t);
void bswm2_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int16_t *, size_t);
void bswm4_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int32_t *, size_t);
void bswm8_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int64_t *, size_t);

static const struct powerpc_bus_space_group multi_strided_ops = {
	bsrm1_s, bsrm2_s, bsrm4_s, bsrm8_s,
	bswm1_s, bswm2_s, bswm4_s, bswm8_s
};

/* read_multi_N */
void bsrm2rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t *,
	size_t);
void bsrm4rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t *,
	size_t);
void bsrm8rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t *,
	size_t);

/* write_multi_N */
void bswm2rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int16_t *, size_t);
void bswm4rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int32_t *, size_t);
void bswm8rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int64_t *, size_t);

static const struct powerpc_bus_space_group multi_rb_strided_ops = {
	bsrm1_s, bsrm2rb_s, bsrm4rb_s, bsrm8rb_s,
	bswm1_s, bswm2rb_s, bswm4rb_s, bswm8rb_s
};

/* read_region_N */
void bsrr1_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int8_t *,
	size_t);
void bsrr2_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t *,
	size_t);
void bsrr4_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t *,
	size_t);
void bsrr8_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t *,
	size_t);

/* write_region_N */
void bswr1_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int8_t *, size_t);
void bswr2_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int16_t *, size_t);
void bswr4_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int32_t *, size_t);
void bswr8_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int64_t *, size_t);

static const struct powerpc_bus_space_group region_strided_ops = {
	bsrr1_s, bsrr2_s, bsrr4_s, bsrr8_s,
	bswr1_s, bswr2_s, bswr4_s, bswr8_s
};

/* read_region_N */
void bsrr2rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t *,
	size_t);
void bsrr4rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t *,
	size_t);
void bsrr8rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t *,
	size_t);

/* write_region_N */
void bswr2rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int16_t *, size_t);
void bswr4rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int32_t *, size_t);
void bswr8rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	const u_int64_t *, size_t);

static const struct powerpc_bus_space_group region_rb_strided_ops = {
	bsrr1_s, bsrr2rb_s, bsrr4rb_s, bsrr8rb_s,
	bswr1_s, bswr2rb_s, bswr4rb_s, bswr8rb_s
};

/* set_region_N */
void bssr1_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int8_t,
	size_t);
void bssr2_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t,
	size_t);
void bssr4_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t,
	size_t);
void bssr8_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t,
	size_t);

static const struct powerpc_bus_space_set set_strided_ops = {
	bssr1_s, bssr2_s, bssr4_s, bssr8_s,
};

/* set_region_N */
void bssr2rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int16_t,
	size_t);
void bssr4rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int32_t,
	size_t);
void bssr8rb_s(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_int64_t,
	size_t);

static const struct powerpc_bus_space_set set_rb_strided_ops = {
	bssr1_s, bssr2rb_s, bssr4rb_s, bssr8rb_s,
};

/* copy_region_N */
void bscr1_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	bus_space_handle_t, bus_size_t, size_t);
void bscr2_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	bus_space_handle_t, bus_size_t, size_t);
void bscr4_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	bus_space_handle_t, bus_size_t, size_t);
void bscr8_s(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	bus_space_handle_t, bus_size_t, size_t);

static const struct powerpc_bus_space_copy copy_strided_ops = {
	bscr1_s, bscr2_s, bscr4_s, bscr8_s
};

static paddr_t memio_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
static int memio_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
	bus_space_handle_t *);
static int memio_subregion(bus_space_tag_t, bus_space_handle_t, bus_size_t,
	bus_size_t, bus_space_handle_t *);
static void memio_unmap(bus_space_tag_t, bus_space_handle_t, bus_size_t);
static int memio_alloc(bus_space_tag_t, bus_addr_t, bus_addr_t, bus_size_t,
	bus_size_t, bus_size_t, int, bus_addr_t *, bus_space_handle_t *);
static void memio_free(bus_space_tag_t, bus_space_handle_t, bus_size_t);
static void memio_barrier(bus_space_tag_t, bus_space_handle_t, bus_size_t,
    bus_size_t, int);

static int extent_flags;

int
bus_space_init(struct powerpc_bus_space *t, const char *extent_name,
	void *storage, size_t storage_size)
{
	if (t->pbs_extent == NULL && extent_name != NULL) {
		t->pbs_extent = extent_create(extent_name, t->pbs_base,
		    t->pbs_limit, storage, storage_size,
		    EX_NOCOALESCE|EX_NOWAIT);
		if (t->pbs_extent == NULL)
			return ENOMEM;
	}

	t->pbs_mmap = memio_mmap;
	t->pbs_map = memio_map;
	t->pbs_subregion = memio_subregion;
	t->pbs_unmap = memio_unmap;
	t->pbs_alloc = memio_alloc;
	t->pbs_free = memio_free;
	t->pbs_barrier = memio_barrier;

	if (t->pbs_flags & _BUS_SPACE_STRIDE_MASK) {
		t->pbs_scalar_stream = scalar_strided_ops;
		t->pbs_multi_stream = &multi_strided_ops;
		t->pbs_region_stream = &region_strided_ops;
		t->pbs_set_stream = &set_strided_ops;
		t->pbs_copy = &copy_strided_ops;
	} else {
		t->pbs_scalar_stream = scalar_ops;
		t->pbs_multi_stream = &multi_ops;
		t->pbs_region_stream = &region_ops;
		t->pbs_set_stream = &set_ops;
		t->pbs_copy = &copy_ops;
	}

#if BYTE_ORDER == BIG_ENDIAN
	if (t->pbs_flags & _BUS_SPACE_BIG_ENDIAN) {
		if (t->pbs_flags & _BUS_SPACE_STRIDE_MASK) {
			t->pbs_scalar = scalar_strided_ops;
			t->pbs_multi = &multi_strided_ops;
			t->pbs_region = &region_strided_ops;
			t->pbs_set = &set_strided_ops;
		} else {
			t->pbs_scalar = scalar_ops;
			t->pbs_multi = &multi_ops;
			t->pbs_region = &region_ops;
			t->pbs_set = &set_ops;
		}
	} else {
		if (t->pbs_flags & _BUS_SPACE_STRIDE_MASK) {
			t->pbs_scalar = scalar_rb_strided_ops;
			t->pbs_multi = &multi_rb_strided_ops;
			t->pbs_region = &region_rb_strided_ops;
			t->pbs_set = &set_rb_strided_ops;
		} else {
			t->pbs_scalar = scalar_rb_ops;
			t->pbs_multi = &multi_rb_ops;
			t->pbs_region = &region_rb_ops;
			t->pbs_set = &set_rb_ops;
		}
	}
#else
	if (t->pbs_flags & _BUS_SPACE_LITTLE_ENDIAN) {
		if (t->pbs_flags & _BUS_SPACE_STRIDE_MASK) {
			t->pbs_scalar = scalar_strided_ops;
			t->pbs_multi = &multi_strided_ops;
			t->pbs_region = &region_strided_ops;
			t->pbs_set = &set_strided_ops;
		} else {
			t->pbs_scalar = scalar_ops;
			t->pbs_multi = &multi_ops;
			t->pbs_region = &region_ops;
			t->pbs_set = &set_ops;
		}
	} else {
		if (t->pbs_flags & _BUS_SPACE_STRIDE_MASK) {
			t->pbs_scalar = scalar_rb_strided_ops;
			t->pbs_multi = &multi_rb_strided_ops;
			t->pbs_region = &region_rb_strided_ops;
			t->pbs_set = &set_rb_strided_ops;
		} else {
			t->pbs_scalar = scalar_rb_ops;
			t->pbs_multi = &multi_rb_ops;
			t->pbs_region = &region_rb_ops;
			t->pbs_set = &set_rb_ops;
		}
	}
#endif
	return 0;
}

void
bus_space_mallocok(void)
{
	extent_flags = EX_MALLOCOK;
}

/* ARGSUSED */
paddr_t
memio_mmap(bus_space_tag_t t, bus_addr_t bpa, off_t offset, int prot, int flags)
{
	paddr_t ret;
	/* XXX what about stride? */
	ret = trunc_page(t->pbs_offset + bpa + offset);

#ifdef DEBUG
	if (ret == 0) {
		printf("%s: [%08x, %08x %08x] mmaps to 0?!\n", __func__,
		    (uint32_t)t->pbs_offset, (uint32_t)bpa, (uint32_t)offset);
		return -1;
	}
#endif

#ifdef POWERPC_MMAP_FLAG_MASK
	if (flags & BUS_SPACE_MAP_PREFETCHABLE)
		ret |= POWERPC_MMAP_FLAG_PREFETCHABLE;
	if (flags & BUS_SPACE_MAP_CACHEABLE)
		ret |= POWERPC_MMAP_FLAG_CACHEABLE;
#endif

	return ret;
}

int
memio_map(bus_space_tag_t t, bus_addr_t bpa, bus_size_t size, int flags,
	bus_space_handle_t *bshp)
{
	int error;
	paddr_t pa;

	size = _BUS_SPACE_STRIDE(t, size);
	bpa = _BUS_SPACE_STRIDE(t, bpa);

	if (t->pbs_limit != 0 && bpa + size - 1 > t->pbs_limit) {
#ifdef DEBUG
		printf("bus_space_map(%p[%x:%x], %#x, %#x) failed: EINVAL\n",
		    t, t->pbs_base, t->pbs_limit, bpa, size);
#endif
		return (EINVAL);
	}

	/*
	 * Can't map I/O space as linear.
	 */
	if ((flags & BUS_SPACE_MAP_LINEAR) &&
	    (t->pbs_flags & _BUS_SPACE_IO_TYPE)) {
		return (EOPNOTSUPP);
	}

	if (t->pbs_extent != NULL) {
#ifdef PPC_IBM4XX
		/*
		 * XXX: Temporary kludge.
		 * Don't bother checking the extent during very early bootstrap.
		 */
		if (extent_flags) {
#endif
			/*
			 * Before we go any further, let's make sure that this
			 * region is available.
			 */
			error = extent_alloc_region(t->pbs_extent, bpa, size,
			    EX_NOWAIT | extent_flags);
			if (error) {
#ifdef DEBUG
				printf("bus_space_map(%p[%x:%x], %#x, %#x)"
				    " failed: %d\n",
				    t, t->pbs_base, t->pbs_limit,
				    bpa, size, error);
#endif
				return (error);
			}
#ifdef PPC_IBM4XX
		}
#endif
	}

	pa = t->pbs_offset + bpa;
#if defined (PPC_OEA) || defined(PPC_OEA601)
#ifdef PPC_OEA601
	if ((mfpvr() >> 16) == MPC601) {
		/*
		 * Map via the MPC601's I/O segments
		 */
		register_t sr = iosrtable[pa >> ADDR_SR_SHFT];
		if (SR601_VALID_P(sr) && ((pa >> ADDR_SR_SHFT) ==
		    ((pa + size - 1) >> ADDR_SR_SHFT))) {
			*bshp = pa;
			return (0);
		}
	} else
#endif /* PPC_OEA601 */
	if ((oeacpufeat & OEACPU_NOBAT) == 0) {
		/*
		 * Let's try to BAT map this address if possible
		 * (note this assumes 1:1 VA:PA)
		 */
		register_t batu = battable[BAT_VA2IDX(pa)].batu;
		if (BAT_VALID_P(batu, 0) && BAT_VA_MATCH_P(batu, pa) &&
		    BAT_VA_MATCH_P(batu, pa + size - 1)) {
			*bshp = pa;
			return (0);
		}
	}
#endif /* defined (PPC_OEA) || defined(PPC_OEA601) */

	/*
	 * Map this into the kernel pmap.
	 */
	*bshp = (bus_space_handle_t) mapiodev(pa, size,
	    (flags & BUS_SPACE_MAP_PREFETCHABLE) != 0);
	if (*bshp == 0) {
		if (t->pbs_extent != NULL) {
			extent_free(t->pbs_extent, bpa, size,
			    EX_NOWAIT | extent_flags);
		}
#ifdef DEBUG
		printf("bus_space_map(%p[%x:%x], %#x, %#x) failed: ENOMEM\n",
		    t, t->pbs_base, t->pbs_limit, bpa, size);
#endif
		return (ENOMEM);
	}

	return (0);
}

int
memio_subregion(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t offset,
	bus_size_t size, bus_space_handle_t *bshp)
{
	*bshp = bsh + _BUS_SPACE_STRIDE(t, offset);
	return (0);
}

void
memio_unmap(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size)
{
	bus_addr_t bpa;
	vaddr_t va = bsh;
	paddr_t pa;

	size = _BUS_SPACE_STRIDE(t, size);

#if defined (PPC_OEA) || defined(PPC_OEA601)
#ifdef PPC_OEA601
	if ((mfpvr() >> 16) == MPC601) {
		register_t sr = iosrtable[va >> ADDR_SR_SHFT];
		if (SR601_VALID_P(sr) && ((va >> ADDR_SR_SHFT) ==
		    ((va + size - 1) >> ADDR_SR_SHFT))) {
			pa = va;
			va = 0;
		} else {
			pmap_extract(pmap_kernel(), va, &pa);
		}
	} else
#endif /* PPC_OEA601 */
	if ((oeacpufeat & OEACPU_NOBAT) == 0) {
		register_t batu = battable[BAT_VA2IDX(va)].batu;
		if (BAT_VALID_P(batu, 0) && BAT_VA_MATCH_P(batu, va) &&
		    BAT_VA_MATCH_P(batu, va + size - 1)) {
			pa = va;
			va = 0;
		} else { 
			pmap_extract(pmap_kernel(), va, &pa);
		}
	} else
		pmap_extract(pmap_kernel(), va, &pa);
#else
	pmap_extract(pmap_kernel(), va, &pa);
#endif /* defined (PPC_OEA) || defined(PPC_OEA601) */
	bpa = pa - t->pbs_offset;

	if (t->pbs_extent != NULL
#ifdef PPC_IBM4XX
	    && extent_flags
#endif
	    && extent_free(t->pbs_extent, bpa, size,
			   EX_NOWAIT | extent_flags)) {
		printf("memio_unmap: %s 0x%lx, size 0x%lx\n",
		    (t->pbs_flags & _BUS_SPACE_IO_TYPE) ? "port" : "mem",
		    (unsigned long)bpa, (unsigned long)size);
		printf("memio_unmap: can't free region\n");
	}

	if (va)
		unmapiodev(va, size);
}

int
memio_alloc(bus_space_tag_t t, bus_addr_t rstart, bus_addr_t rend,
	bus_size_t size, bus_size_t alignment, bus_size_t boundary,
	int flags, bus_addr_t *bpap, bus_space_handle_t *bshp)
{
	u_long bpa;
	paddr_t pa;
	int error;

	size = _BUS_SPACE_STRIDE(t, size);
	rstart = _BUS_SPACE_STRIDE(t, rstart);

	if (t->pbs_extent == NULL)
		return ENOMEM;

	if (t->pbs_limit != 0 && rstart + size - 1 > t->pbs_limit) {
#ifdef DEBUG
		printf("%s(%p[%x:%x], %#x, %#x) failed: EINVAL\n",
		   __func__, t, t->pbs_base, t->pbs_limit, rstart, size);
#endif
		return (EINVAL);
	}

	/*
	 * Can't map I/O space as linear.
	 */
	if ((flags & BUS_SPACE_MAP_LINEAR) &&
	    (t->pbs_flags & _BUS_SPACE_IO_TYPE))
		return (EOPNOTSUPP);

	if (rstart < t->pbs_extent->ex_start || rend > t->pbs_extent->ex_end)
		panic("memio_alloc: bad region start/end");

	error = extent_alloc_subregion(t->pbs_extent, rstart, rend, size,
	    alignment, boundary, EX_FAST | EX_NOWAIT | extent_flags, &bpa);

	if (error)
		return (error);

	*bpap = bpa;
	pa = t->pbs_offset + bpa;
#if defined (PPC_OEA) || defined(PPC_OEA601)
#ifdef PPC_OEA601
	if ((mfpvr() >> 16) == MPC601) {
		register_t sr = iosrtable[pa >> ADDR_SR_SHFT];
		if (SR601_VALID_P(sr) && SR601_PA_MATCH_P(sr, pa) &&
		    SR601_PA_MATCH_P(sr, pa + size - 1)) {
			*bshp = pa;
			return (0);
		}
	} else
#endif /* PPC_OEA601 */
	if ((oeacpufeat & OEACPU_NOBAT) == 0) {
		register_t batu = battable[BAT_VA2IDX(pa)].batu;
		if (BAT_VALID_P(batu, 0) && BAT_VA_MATCH_P(batu, pa) &&
		    BAT_VA_MATCH_P(batu, pa + size - 1)) {
			*bshp = pa;
			return (0);
		}
	}
#endif /* defined (PPC_OEA) || defined(PPC_OEA601) */
	*bshp = (bus_space_handle_t) mapiodev(pa, size, false);
	if (*bshp == 0) {
		extent_free(t->pbs_extent, bpa, size, EX_NOWAIT | extent_flags);
		return (ENOMEM);
	}

	return (0);
}

void
memio_free(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size)
{
	if (t->pbs_extent == NULL)
		return;

	/* memio_unmap() does all that we need to do. */
	memio_unmap(t, bsh, size);
}

void
memio_barrier(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size,
    bus_size_t offset, int flags)
{
	__asm volatile("eieio" ::: "memory");
}