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
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
/*	$NetBSD: ofw.c,v 1.68 2018/09/03 16:29:27 riastradh Exp $	*/

/*
 * Copyright 1997
 * Digital Equipment Corporation. All rights reserved.
 *
 * This software is furnished under license and may be used and
 * copied only in accordance with the following terms and conditions.
 * Subject to these conditions, you may download, copy, install,
 * use, modify and distribute this software in source and/or binary
 * form. No title or ownership is transferred hereby.
 *
 * 1) Any source code used, modified or distributed must reproduce
 *    and retain this copyright notice and list of conditions as
 *    they appear in the source file.
 *
 * 2) No right is granted to use any trade name, trademark, or logo of
 *    Digital Equipment Corporation. Neither the "Digital Equipment
 *    Corporation" name nor any trademark or logo of Digital Equipment
 *    Corporation may be used to endorse or promote products derived
 *    from this software without the prior written permission of
 *    Digital Equipment Corporation.
 *
 * 3) This software is provided "AS-IS" and any express or implied
 *    warranties, including but not limited to, any implied warranties
 *    of merchantability, fitness for a particular purpose, or
 *    non-infringement are disclaimed. In no event shall DIGITAL be
 *    liable for any damages whatsoever, and in particular, DIGITAL
 *    shall not be liable for special, indirect, consequential, or
 *    incidental damages or damages for lost profits, loss of
 *    revenue or loss of use, whether such damages arise in contract,
 *    negligence, tort, under statute, in equity, at law or otherwise,
 *    even if advised of the possibility of such damage.
 */

/*
 *  Routines for interfacing between NetBSD and OFW.
 * 
 *  Parts of this could be moved to an MI file in time. -JJK
 *
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ofw.c,v 1.68 2018/09/03 16:29:27 riastradh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/reboot.h>
#include <sys/mbuf.h>
#include <sys/cpu.h>
#include <sys/intr.h>

#include <uvm/uvm.h>

#include <dev/cons.h>

#define	_ARM32_BUS_DMA_PRIVATE
#include <sys/bus.h>

#include <arm/locore.h>

#include <machine/bootconfig.h>
#include <machine/irqhandler.h>

#include <dev/ofw/openfirm.h>
#include <machine/ofw.h>

#include <netinet/in.h>

#if	BOOT_FW_DHCP
#include <nfs/bootdata.h>
#endif

#ifdef SHARK
#include "machine/pio.h"
#include "machine/isa_machdep.h"
#endif

#include "isadma.h"
#include "igsfb_ofbus.h"
#include "chipsfb_ofbus.h"
#include "vga_ofbus.h"

#define IO_VIRT_BASE (OFW_VIRT_BASE + OFW_VIRT_SIZE)
#define IO_VIRT_SIZE 0x01000000

#define	KERNEL_IMG_PTS		2
#define	KERNEL_VMDATA_PTS	(KERNEL_VM_SIZE >> (L1_S_SHIFT + 2))
#define	KERNEL_OFW_PTS		4
#define	KERNEL_IO_PTS		4

#define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
/*
 * The range 0xf1000000 - 0xf6ffffff is available for kernel VM space
 * OFW sits at 0xf7000000
 */
#define	KERNEL_VM_SIZE		0x06000000

/*
 *  Imported variables
 */
extern BootConfig bootconfig;	/* temporary, I hope */

#ifdef	DIAGNOSTIC
/* NOTE: These variables will be removed, well some of them */
extern u_int current_mask;
#endif

extern int ofw_handleticks;


/*
 *  Imported routines
 */
extern void dump_spl_masks(void);
extern void dumpsys(void);
extern void dotickgrovelling(vaddr_t);

#define WriteWord(a, b) \
*((volatile unsigned int *)(a)) = (b)

#define ReadWord(a) \
(*((volatile unsigned int *)(a)))


/*
 *  Exported variables
 */
/* These should all be in a meminfo structure. */
paddr_t physical_start;
paddr_t physical_freestart;
paddr_t physical_freeend;
paddr_t physical_end;
u_int free_pages;

paddr_t msgbufphys;

/* for storage allocation, used to be local to ofw_construct_proc0_addrspace */
static vaddr_t  virt_freeptr;

int ofw_callbacks = 0;		/* debugging counter */

#if (NIGSFB_OFBUS > 0) || (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
int console_ihandle = 0;
static void reset_screen(void);
#endif

/**************************************************************/


/*
 *  Declarations and definitions private to this module
 *
 */

struct mem_region {
	paddr_t start;
	psize_t size;
};

struct mem_translation {
	vaddr_t virt;
	vsize_t size;
	paddr_t phys;
	unsigned int mode;
};

struct isa_range {
	paddr_t isa_phys_hi;
	paddr_t isa_phys_lo;
	paddr_t parent_phys_start;
	psize_t isa_size;
};

struct vl_range {
	paddr_t vl_phys_hi;
	paddr_t vl_phys_lo;
	paddr_t parent_phys_start;
	psize_t vl_size;
};

struct vl_isa_range {
	paddr_t isa_phys_hi;
	paddr_t isa_phys_lo;
	paddr_t parent_phys_hi;
	paddr_t parent_phys_lo;
	psize_t isa_size;
};

struct dma_range {
	paddr_t start;
	psize_t   size;
};

struct ofw_cbargs {
	char *name;
	int nargs;
	int nreturns;
	int args_n_results[12];
};


/* Memory info */
static int nOFphysmem;
static struct mem_region *OFphysmem;
static int nOFphysavail;
static struct mem_region *OFphysavail;
static int nOFtranslations;
static struct mem_translation *OFtranslations;
static int nOFdmaranges;
static struct dma_range *OFdmaranges;

/* The OFW client services handle. */
/* Initialized by ofw_init(). */
static ofw_handle_t ofw_client_services_handle;


static void ofw_callbackhandler(void *);
static void ofw_construct_proc0_addrspace(void);
static void ofw_getphysmeminfo(void);
static void ofw_getvirttranslations(void);
static void *ofw_malloc(vsize_t size);
static void ofw_claimpages(vaddr_t *, pv_addr_t *, vsize_t);
static void ofw_discardmappings(vaddr_t, vaddr_t, vsize_t);
static int ofw_mem_ihandle(void);
static int ofw_mmu_ihandle(void);
static paddr_t ofw_claimphys(paddr_t, psize_t, paddr_t);
#if 0
static paddr_t ofw_releasephys(paddr_t, psize_t);
#endif
static vaddr_t ofw_claimvirt(vaddr_t, vsize_t, vaddr_t);
static void ofw_settranslation(vaddr_t, paddr_t, vsize_t, int);
static void ofw_initallocator(void);
static void ofw_configisaonly(paddr_t *, paddr_t *);
static void ofw_configvl(int, paddr_t *, paddr_t *);
static vaddr_t ofw_valloc(vsize_t, vaddr_t);


/*
 * DHCP hooks.  For a first cut, we look to see if there is a DHCP
 * packet that was saved by the firmware.  If not, we proceed as before,
 * getting hand-configured data from NVRAM.  If there is one, we get the
 * packet, and extract the data from it.  For now, we hand that data up
 * in the boot_args string as before.
 */


/**************************************************************/


/*
 *
 *  Support routines for xxx_machdep.c
 *
 *  The intent is that all OFW-based configurations use the
 *  exported routines in this file to do their business.  If
 *  they need to override some function they are free to do so.
 *
 *  The exported routines are:
 *
 *    openfirmware
 *    ofw_init
 *    ofw_boot
 *    ofw_getbootinfo
 *    ofw_configmem
 *    ofw_configisa
 *    ofw_configisadma
 *    ofw_gettranslation
 *    ofw_map
 *    ofw_getcleaninfo
 */


int
openfirmware(void *args)
{
	int ofw_result;
	u_int saved_irq_state;

	/* OFW is not re-entrant, so we wrap a mutex around the call. */
	saved_irq_state = disable_interrupts(I32_bit);
	ofw_result = ofw_client_services_handle(args);
	(void)restore_interrupts(saved_irq_state);

	return(ofw_result);
}


void
ofw_init(ofw_handle_t ofw_handle)
{
	ofw_client_services_handle = ofw_handle;

	/*  Everything we allocate in the remainder of this block is
	 *  constrained to be in the "kernel-static" portion of the
	 *  virtual address space (i.e., 0xF0000000 - 0xF1000000).
	 *  This is because all such objects are expected to be in
	 *  that range by NetBSD, or the objects will be re-mapped
	 *  after the page-table-switch to other specific locations.
	 *  In the latter case, it's simplest if our pre-switch handles
	 *  on those objects are in regions that are already "well-
	 *  known."  (Otherwise, the cloning of the OFW-managed address-
	 *  space becomes more awkward.)  To minimize the number of L2
	 *  page tables that we use, we are further restricting the
	 *  remaining allocations in this block to the bottom quarter of
	 *  the legal range.  OFW will have loaded the kernel text+data+bss
	 *  starting at the bottom of the range, and we will allocate
	 *  objects from the top, moving downwards.  The two sub-regions
	 *  will collide if their total sizes hit 8MB.  The current total
	 *  is <1.5MB, but INSTALL kernels are > 4MB, so hence the 8MB
	 *  limit.  The variable virt-freeptr represents the next free va
	 *  (moving downwards).
	 */
	virt_freeptr = KERNEL_BASE + (0x00400000 * KERNEL_IMG_PTS);
}


void
ofw_boot(int howto, char *bootstr)
{

#ifdef DIAGNOSTIC
	printf("boot: howto=%08x curlwp=%p\n", howto, curlwp);
	printf("current_mask=%08x\n", current_mask);

	printf("ipl_bio=%08x ipl_net=%08x ipl_tty=%08x ipl_vm=%08x\n",
	    irqmasks[IPL_BIO], irqmasks[IPL_NET], irqmasks[IPL_TTY],
	    irqmasks[IPL_VM]);
	printf("ipl_clock=%08x ipl_none=%08x\n",
	    irqmasks[IPL_CLOCK], irqmasks[IPL_NONE]);

	dump_spl_masks();
#endif

	/*
	 * If we are still cold then hit the air brakes
	 * and crash to earth fast
	 */
	if (cold) {
		doshutdownhooks();
		pmf_system_shutdown(boothowto);
		printf("Halted while still in the ICE age.\n");
		printf("The operating system has halted.\n");
		goto ofw_exit;
		/*NOTREACHED*/
	}

	/*
	 * If RB_NOSYNC was not specified sync the discs.
	 * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
	 * It looks like syslogd is getting woken up only to find that it cannot
	 * page part of the binary in as the filesystem has been unmounted.
	 */
	if (!(howto & RB_NOSYNC))
		bootsync();

	/* Say NO to interrupts */
	splhigh();

	/* Do a dump if requested. */
	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
		dumpsys();
	
	/* Run any shutdown hooks */
	doshutdownhooks();

	pmf_system_shutdown(boothowto);

	/* Make sure IRQ's are disabled */
	IRQdisable;

	if (howto & RB_HALT) {
		printf("The operating system has halted.\n");
		goto ofw_exit;
	}

	/* Tell the user we are booting */
	printf("rebooting...\n");

	/* Jump into the OFW boot routine. */
	{
		static char str[256];
		char *ap = str, *ap1 = ap;

		if (bootstr && *bootstr) {
			if (strlen(bootstr) > sizeof str - 5)
				printf("boot string too large, ignored\n");
			else {
				strcpy(str, bootstr);
				ap1 = ap = str + strlen(str);
				*ap++ = ' ';
			}
		}
		*ap++ = '-';
		if (howto & RB_SINGLE)
			*ap++ = 's';
		if (howto & RB_KDB)
			*ap++ = 'd';
		*ap++ = 0;
		if (ap[-2] == '-')
			*ap1 = 0;
#if (NIGSFB_OFBUS > 0) || (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
		reset_screen();
#endif
		OF_boot(str);
		/*NOTREACHED*/
	}

ofw_exit:
	printf("Calling OF_exit...\n");
#if (NIGSFB_OFBUS > 0) || (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
	reset_screen();
#endif
	OF_exit();
	/*NOTREACHED*/
}


#if	BOOT_FW_DHCP

extern	char	*ip2dotted(struct in_addr);

/*
 * Get DHCP data from OFW
 */

void
get_fw_dhcp_data(struct bootdata *bdp)
{
	int chosen;
	int dhcplen;

	memset((char *)bdp, 0, sizeof(*bdp));
	if ((chosen = OF_finddevice("/chosen")) == -1)
		panic("no /chosen from OFW");
	if ((dhcplen = OF_getproplen(chosen, "bootp-response")) > 0) {
		u_char *cp;
		int dhcp_type = 0;
		char *ip;

		/*
		 * OFW saved a DHCP (or BOOTP) packet for us.
		 */
		if (dhcplen > sizeof(bdp->dhcp_packet))
			panic("DHCP packet too large");
		OF_getprop(chosen, "bootp-response", &bdp->dhcp_packet,
		    sizeof(bdp->dhcp_packet));
		SANITY(bdp->dhcp_packet.op == BOOTREPLY, "bogus DHCP packet");
		/*
		 * Collect the interesting data from DHCP into
		 * the bootdata structure.
		 */
		bdp->ip_address = bdp->dhcp_packet.yiaddr;
		ip = ip2dotted(bdp->ip_address);
		if (memcmp(bdp->dhcp_packet.options, DHCP_OPTIONS_COOKIE, 4) == 0)
			parse_dhcp_options(&bdp->dhcp_packet,
			    bdp->dhcp_packet.options + 4,
			    &bdp->dhcp_packet.options[dhcplen
			    - DHCP_FIXED_NON_UDP], bdp, ip);
		if (bdp->root_ip.s_addr == 0)
			bdp->root_ip = bdp->dhcp_packet.siaddr;
		if (bdp->swap_ip.s_addr == 0)
			bdp->swap_ip = bdp->dhcp_packet.siaddr;
	}
	/*
	 * If the DHCP packet did not contain all the necessary data,
	 * look in NVRAM for the missing parts.
	 */
	{
		int options;
		int proplen;
#define BOOTJUNKV_SIZE	256
		char bootjunkv[BOOTJUNKV_SIZE];	/* minimize stack usage */


		if ((options = OF_finddevice("/options")) == -1)
			panic("can't find /options");
		if (bdp->ip_address.s_addr == 0 &&
		    (proplen = OF_getprop(options, "ipaddr",
		    bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) {
			bootjunkv[proplen] = '\0';
			if (dotted2ip(bootjunkv, &bdp->ip_address.s_addr) == 0)
				bdp->ip_address.s_addr = 0;
		}
		if (bdp->ip_mask.s_addr == 0 &&
		    (proplen = OF_getprop(options, "netmask",
		    bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) {
			bootjunkv[proplen] = '\0';
			if (dotted2ip(bootjunkv, &bdp->ip_mask.s_addr) == 0)
				bdp->ip_mask.s_addr = 0;
		}
		if (bdp->hostname[0] == '\0' &&
		    (proplen = OF_getprop(options, "hostname",
		    bdp->hostname, sizeof(bdp->hostname) - 1)) > 0) {
			bdp->hostname[proplen] = '\0';
		}
		if (bdp->root[0] == '\0' &&
		    (proplen = OF_getprop(options, "rootfs",
		    bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) {
			bootjunkv[proplen] = '\0';
			parse_server_path(bootjunkv, &bdp->root_ip, bdp->root);
		}
		if (bdp->swap[0] == '\0' &&
		    (proplen = OF_getprop(options, "swapfs",
		    bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) {
			bootjunkv[proplen] = '\0';
			parse_server_path(bootjunkv, &bdp->swap_ip, bdp->swap);
		}
	}
}

#endif	/* BOOT_FW_DHCP */

void
ofw_getbootinfo(char **bp_pp, char **ba_pp)
{
	int chosen;
	int bp_len;
	int ba_len;
	char *bootpathv;
	char *bootargsv;

	/* Read the bootpath and bootargs out of OFW. */
	/* XXX is bootpath still interesting?  --emg */
	if ((chosen = OF_finddevice("/chosen")) == -1)
		panic("no /chosen from OFW");
	bp_len = OF_getproplen(chosen, "bootpath");
	ba_len = OF_getproplen(chosen, "bootargs");
	if (bp_len < 0 || ba_len < 0)
		panic("can't get boot data from OFW");

	bootpathv = (char *)ofw_malloc(bp_len);
	bootargsv = (char *)ofw_malloc(ba_len);

	if (bp_len)
		OF_getprop(chosen, "bootpath", bootpathv, bp_len);
	else
		bootpathv[0] = '\0';

	if (ba_len)
		OF_getprop(chosen, "bootargs", bootargsv, ba_len);
	else
		bootargsv[0] = '\0';

	*bp_pp = bootpathv;
	*ba_pp = bootargsv;
#ifdef DIAGNOSTIC
	printf("bootpath=<%s>, bootargs=<%s>\n", bootpathv, bootargsv);
#endif
}

paddr_t
ofw_getcleaninfo(void)
{
	int cpu;
	vaddr_t vclean;
	paddr_t pclean;

	if ((cpu = OF_finddevice("/cpu")) == -1)
		panic("no /cpu from OFW");

	if ((OF_getprop(cpu, "d-cache-flush-address", &vclean, 
	    sizeof(vclean))) != sizeof(vclean)) {
#ifdef DEBUG
		printf("no OFW d-cache-flush-address property\n");
#endif
		return -1;
	}

	if ((pclean = ofw_gettranslation(
	    of_decode_int((unsigned char *)&vclean))) == -1)
	panic("OFW failed to translate cache flush address");

	return pclean;
}

void
ofw_configisa(paddr_t *pio, paddr_t *pmem)
{
	int vl;

	if ((vl = OF_finddevice("/vlbus")) == -1) /* old style OFW dev info tree */
		ofw_configisaonly(pio, pmem);
	else /* old style OFW dev info tree */
		ofw_configvl(vl, pio, pmem);
}

static void
ofw_configisaonly(paddr_t *pio, paddr_t *pmem)
{
	int isa;
	int rangeidx;
	int size;
	paddr_t hi, start;
	struct isa_range ranges[2];
  
	if ((isa = OF_finddevice("/isa")) == -1)
	panic("OFW has no /isa device node");

	/* expect to find two isa ranges: IO/data and memory/data */
	if ((size = OF_getprop(isa, "ranges", ranges, sizeof(ranges))) 
	    != sizeof(ranges))
		panic("unexpected size of OFW /isa ranges property: %d", size);

	*pio = *pmem = -1;

	for (rangeidx = 0; rangeidx < 2; ++rangeidx) {
		hi    = of_decode_int((unsigned char *)
		    &ranges[rangeidx].isa_phys_hi);
		start = of_decode_int((unsigned char *)
		    &ranges[rangeidx].parent_phys_start);

	if (hi & 1) { /* then I/O space */
		*pio = start;
	} else {
		*pmem = start;
	}
	} /* END for */

	if ((*pio == -1) || (*pmem == -1))
		panic("bad OFW /isa ranges property");

}

static void
ofw_configvl(int vl, paddr_t *pio, paddr_t *pmem)
{
	int isa;
	int ir, vr;
	int size;
	paddr_t hi, start;
	struct vl_isa_range isa_ranges[2];
	struct vl_range     vl_ranges[2];
  
	if ((isa = OF_finddevice("/vlbus/isa")) == -1)
		panic("OFW has no /vlbus/isa device node");

	/* expect to find two isa ranges: IO/data and memory/data */
	if ((size = OF_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))) 
	    != sizeof(isa_ranges))
		panic("unexpected size of OFW /vlbus/isa ranges property: %d",
		     size);

	/* expect to find two vl ranges: IO/data and memory/data */
	if ((size = OF_getprop(vl, "ranges", vl_ranges, sizeof(vl_ranges))) 
	    != sizeof(vl_ranges))
		panic("unexpected size of OFW /vlbus ranges property: %d", size);

	*pio = -1;
	*pmem = -1;

	for (ir = 0; ir < 2; ++ir) {
		for (vr = 0; vr < 2; ++vr) {
			if ((isa_ranges[ir].parent_phys_hi
			    == vl_ranges[vr].vl_phys_hi) &&
			    (isa_ranges[ir].parent_phys_lo
			    == vl_ranges[vr].vl_phys_lo)) {
				hi    = of_decode_int((unsigned char *)
				    &isa_ranges[ir].isa_phys_hi);
				start = of_decode_int((unsigned char *)
				    &vl_ranges[vr].parent_phys_start);

				if (hi & 1) { /* then I/O space */
					*pio = start;
				} else {
					*pmem = start;
				}
			} /* END if */
		} /* END for */
	} /* END for */

	if ((*pio == -1) || (*pmem == -1))
		panic("bad OFW /isa ranges property");
}

#if NISADMA > 0
struct arm32_dma_range *shark_isa_dma_ranges;
int shark_isa_dma_nranges;
#endif

void
ofw_configisadma(paddr_t *pdma)
{
	int root;
	int rangeidx;
	int size;
	struct dma_range *dr;

	if ((root = OF_finddevice("/")) == -1 ||
	    (size = OF_getproplen(root, "dma-ranges")) <= 0 ||
	    (OFdmaranges = (struct dma_range *)ofw_malloc(size)) == 0 ||
 	    OF_getprop(root, "dma-ranges", OFdmaranges, size) != size)
		panic("bad / dma-ranges property");

	nOFdmaranges = size / sizeof(struct dma_range);

#if NISADMA > 0
	/* Allocate storage for non-OFW representation of the range. */
	shark_isa_dma_ranges = ofw_malloc(nOFdmaranges *
	    sizeof(*shark_isa_dma_ranges));
	if (shark_isa_dma_ranges == NULL)
		panic("unable to allocate shark_isa_dma_ranges");
	shark_isa_dma_nranges = nOFdmaranges;
#endif

	for (rangeidx = 0, dr = OFdmaranges; rangeidx < nOFdmaranges;
	    ++rangeidx, ++dr) {
		dr->start = of_decode_int((unsigned char *)&dr->start);
		dr->size = of_decode_int((unsigned char *)&dr->size);
#if NISADMA > 0
		shark_isa_dma_ranges[rangeidx].dr_sysbase = dr->start;
		shark_isa_dma_ranges[rangeidx].dr_busbase = dr->start;
		shark_isa_dma_ranges[rangeidx].dr_len  = dr->size;
#endif
	}

#ifdef DEBUG
	printf("DMA ranges size = %d\n", size);

	for (rangeidx = 0; rangeidx < nOFdmaranges; ++rangeidx) {
		printf("%08lx %08lx\n", 
		(u_long)OFdmaranges[rangeidx].start, 
		(u_long)OFdmaranges[rangeidx].size);
	}
#endif
}

/* 
 *  Memory configuration:
 *
 *  We start off running in the environment provided by OFW.
 *  This has the MMU turned on, the kernel code and data
 *  mapped-in at KERNEL_BASE (0xF0000000), OFW's text and
 *  data mapped-in at OFW_VIRT_BASE (0xF7000000), and (possibly)
 *  page0 mapped-in at 0x0.
 *
 *  The strategy is to set-up the address space for proc0 --
 *  including the allocation of space for new page tables -- while
 *  memory is still managed by OFW.  We then effectively create a
 *  copy of the address space by dumping all of OFW's translations
 *  and poking them into the new page tables.  We then notify OFW
 *  that we are assuming control of memory-management by installing
 *  our callback-handler, and switch to the NetBSD-managed page
 *  tables with the cpu_setttb() call.
 *  
 *  This scheme may cause some amount of memory to be wasted within
 *  OFW as dead page tables, but it shouldn't be more than about 
 *  20-30KB.  (It's also possible that OFW will re-use the space.)
 */
void
ofw_configmem(void)
{
	int i;

	/* Set-up proc0 address space. */
	ofw_construct_proc0_addrspace();

	/*
	 * Get a dump of OFW's picture of physical memory.
	 * This is used below to initialize a load of variables used by pmap.
	 * We get it now rather than later because we are about to
	 * tell OFW to stop managing memory.
	 */
	ofw_getphysmeminfo();

	/* We are about to take control of memory-management from OFW.
	 * Establish callbacks for OFW to use for its future memory needs.
	 * This is required for us to keep using OFW services.
	 */

	/* First initialize our callback memory allocator. */
	ofw_initallocator();

	OF_set_callback(ofw_callbackhandler);

	/* Switch to the proc0 pagetables. */
	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
	cpu_setttb(kernel_l1pt.pv_pa, true);
	cpu_tlb_flushID();
	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));

	/*
	 * Moved from cpu_startup() as data_abort_handler() references
	 * this during uvm init
	 */
	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);

	/* Set-up the various globals which describe physical memory for pmap. */
	{
		struct mem_region *mp;
		int totalcnt;
		int availcnt;

		/* physmem, physical_start, physical_end */
		physmem = 0;
		for (totalcnt = 0, mp = OFphysmem; totalcnt < nOFphysmem;
		    totalcnt++, mp++) {
#ifdef	OLDPRINTFS
			printf("physmem: %x, %x\n", mp->start, mp->size);
#endif
			physmem += btoc(mp->size);
		}
		physical_start = OFphysmem[0].start;
		mp--;
		physical_end = mp->start + mp->size;

		/* free_pages, physical_freestart, physical_freeend */
		free_pages = 0;
		for (availcnt = 0, mp = OFphysavail; availcnt < nOFphysavail;
		    availcnt++, mp++) {
#ifdef	OLDPRINTFS
			printf("physavail: %x, %x\n", mp->start, mp->size);
#endif
			free_pages += btoc(mp->size);
		}
		physical_freestart = OFphysavail[0].start;
		mp--;
		physical_freeend = mp->start + mp->size;
#ifdef	OLDPRINTFS
		printf("pmap_bootstrap:  physmem = %x, free_pages = %x\n",
		    physmem, free_pages);
#endif

		/*
		 *  This is a hack to work with the existing pmap code.
		 *  That code depends on a RiscPC BootConfig structure
		 *  containing, among other things, an array describing
		 *  the regions of physical memory.  So, for now, we need
		 *  to stuff our OFW-derived physical memory info into a
		 *  "fake" BootConfig structure.
		 *
		 *  An added twist is that we initialize the BootConfig
		 *  structure with our "available" physical memory regions
		 *  rather than the "total" physical memory regions.  Why?
		 *  Because:
		 *
		 *   (a) the VM code requires that the "free" pages it is
		 *       initialized with have consecutive indices.  This
		 *       allows it to use more efficient data structures
		 *       (presumably).
		 *   (b) the current pmap routines which report the initial
		 *       set of free page indices (pmap_next_page) and
		 *       which map addresses to indices (pmap_page_index)
		 *       assume that the free pages are consecutive across
		 *       memory region boundaries.
		 *
		 *  This means that memory which is "stolen" at startup time
		 *  (say, for page descriptors) MUST come from either the
		 *  bottom of the first region or the top of the last.
		 *
		 *  This requirement doesn't mesh well with OFW (or at least
		 *  our use of it).  We can get around it for the time being
		 *  by pretending that our "available" region array describes
		 *  all of our physical memory.  This may cause some important
		 *  information to be excluded from a dump file, but so far
		 *  I haven't come across any other negative effects.
		 *
		 *  In the long-run we should fix the index
		 *  generation/translation code in the pmap module.
		 */

		if (DRAM_BLOCKS < (availcnt + 1))
			panic("more ofw memory regions than bootconfig blocks");

		for (i = 0, mp = OFphysavail; i < nOFphysavail; i++, mp++) {
			bootconfig.dram[i].address = mp->start;
			bootconfig.dram[i].pages = btoc(mp->size);
		}
		bootconfig.dramblocks = availcnt;
	}

	uvm_md_init();

	/* XXX Please kill this code dead. */
	for (i = 0; i < bootconfig.dramblocks; i++) {
		paddr_t start = (paddr_t)bootconfig.dram[i].address;
		paddr_t end = start + (bootconfig.dram[i].pages * PAGE_SIZE);
#if NISADMA > 0
		paddr_t istart, isize;
#endif

		if (start < physical_freestart)
			start = physical_freestart;
		if (end > physical_freeend)
			end = physical_freeend;

#if 0
		printf("%d: %lx -> %lx\n", loop, start, end - 1);
#endif

#if NISADMA > 0
		if (arm32_dma_range_intersect(shark_isa_dma_ranges,
					      shark_isa_dma_nranges,
					      start, end - start,
					      &istart, &isize)) {
			/*
			 * Place the pages that intersect with the
			 * ISA DMA range onto the ISA DMA free list.
			 */
#if 0
			printf("    ISADMA 0x%lx -> 0x%lx\n", istart,
			    istart + isize - 1);
#endif
			uvm_page_physload(atop(istart),
			    atop(istart + isize), atop(istart),
			    atop(istart + isize), VM_FREELIST_ISADMA);

			/*
			 * Load the pieces that come before the
			 * intersection onto the default free list.
			 */
			if (start < istart) {
#if 0
				printf("    BEFORE 0x%lx -> 0x%lx\n",
				    start, istart - 1);
#endif
				uvm_page_physload(atop(start),
				    atop(istart), atop(start),
				    atop(istart), VM_FREELIST_DEFAULT);
			}

			/*
			 * Load the pieces that come after the
			 * intersection onto the default free list.
			 */
			if ((istart + isize) < end) {
#if 0
				printf("     AFTER 0x%lx -> 0x%lx\n",
				    (istart + isize), end - 1);
#endif
				uvm_page_physload(atop(istart + isize),
				    atop(end), atop(istart + isize),
				    atop(end), VM_FREELIST_DEFAULT);
			}
		} else {
			uvm_page_physload(atop(start), atop(end),
			    atop(start), atop(end), VM_FREELIST_DEFAULT);
		}
#else /* NISADMA > 0 */
		uvm_page_physload(atop(start), atop(end),
		    atop(start), atop(end), VM_FREELIST_DEFAULT);
#endif /* NISADMA > 0 */
	}

	/* Initialize pmap module. */
	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
}


/*
 ************************************************************

  Routines private to this module

 ************************************************************
 */

/* N.B.  Not supposed to call printf in callback-handler!  Could deadlock! */
static void
ofw_callbackhandler(void *v)
{
	struct ofw_cbargs *args = v;
	char *name = args->name;
	int nargs = args->nargs;
	int nreturns = args->nreturns;
	int *args_n_results = args->args_n_results;

	ofw_callbacks++;

#if defined(OFWGENCFG)
	/* Check this first, so that we don't waste IRQ time parsing. */
	if (strcmp(name, "tick") == 0) {
		vaddr_t frame;

		/* Check format. */
		if (nargs != 1 || nreturns < 1) {
			args_n_results[nargs] = -1;
			args->nreturns = 1;
			return;
		}
		args_n_results[nargs] =	0;	/* properly formatted request */

		/*
		 *  Note that we are running in the IRQ frame, with interrupts
		 *  disabled.
		 *  
		 *  We need to do two things here:
		 *    - copy a few words out of the input frame into a global
		 *      area, for later use by our real tick-handling code
		 *    - patch a few words in the frame so that when OFW returns
		 *      from the interrupt it will resume with our handler
		 *      rather than the code that was actually interrupted.
		 *      Our handler will resume when it finishes with the code
		 *      that was actually interrupted.
		 *  
		 *  It's simplest to do this in assembler, since it requires
		 *  switching frames and grovelling about with registers.
		 */
		frame = (vaddr_t)args_n_results[0];
		if (ofw_handleticks)
			dotickgrovelling(frame);
		args_n_results[nargs + 1] = frame;
		args->nreturns = 1;
	} else
#endif

	if (strcmp(name, "map") == 0) {
		vaddr_t va;
		paddr_t pa;
		vsize_t size;
		int mode;
		int ap_bits;
		int dom_bits;
		int cb_bits;

		/* Check format. */
		if (nargs != 4 || nreturns < 2) {
			args_n_results[nargs] = -1;
			args->nreturns = 1;
			return;
		}
		args_n_results[nargs] =	0;	/* properly formatted request */

		pa = (paddr_t)args_n_results[0];
		va = (vaddr_t)args_n_results[1];
		size = (vsize_t)args_n_results[2];
		mode = args_n_results[3];
		ap_bits =  (mode & 0x00000C00);
		dom_bits = (mode & 0x000001E0);
		cb_bits =  (mode & 0x000000C0);

		/* Sanity checks. */
		if ((va & PGOFSET) != 0 || va < OFW_VIRT_BASE ||
		    (va + size) > (OFW_VIRT_BASE + OFW_VIRT_SIZE) ||
		    (pa & PGOFSET) != 0 || (size & PGOFSET) != 0 ||
		    size == 0 || (dom_bits >> 5) != 0) {
			args_n_results[nargs + 1] = -1;
			args->nreturns = 1;
			return;
		}

		/* Write-back anything stuck in the cache. */
		cpu_idcache_wbinv_all();

		/* Install new mappings. */
		{
			pt_entry_t *ptep = vtopte(va);
			KASSERT(ptep + size / L2_S_SIZE == vtopte(va + size));
			pt_entry_t npte = pa | L2_TYPE_S | L2_AP(ap_bits)
			    | cb_bits;
			
			ap_bits >>= 10;
			for (size_t npages = size >> PGSHIFT;
			     npages-- > 0;
			     ptep += PAGE_SIZE / L2_S_SIZE, npte += PAGE_SIZE) {
				l2pte_set(ptep, npte, 0);
			}
			PTE_SYNC_RANGE(vtopte(va), size >> L2_S_SHIFT);
		}

		/* Clean out tlb. */
		cpu_tlb_flushID();

		args_n_results[nargs + 1] = 0;
		args->nreturns = 2;
	} else if (strcmp(name, "unmap") == 0) {
		vaddr_t va;
		vsize_t size;

		/* Check format. */
		if (nargs != 2 || nreturns < 1) {
			args_n_results[nargs] = -1;
			args->nreturns = 1;
			return;
		}
		args_n_results[nargs] =	0;	/* properly formatted request */

		va = (vaddr_t)args_n_results[0];
		size = (vsize_t)args_n_results[1];

		/* Sanity checks. */
		if ((va & PGOFSET) != 0 || va < OFW_VIRT_BASE ||
		    (va + size) > (OFW_VIRT_BASE + OFW_VIRT_SIZE) ||
		    (size & PGOFSET) != 0 || size == 0) {
			args_n_results[nargs + 1] = -1;
			args->nreturns = 1;
			return;
		}

		/* Write-back anything stuck in the cache. */
		cpu_idcache_wbinv_all();

		/* Zero the mappings. */
		{
			pt_entry_t *ptep = vtopte(va);
			
			for (size_t npages = size >> PGSHIFT;
			     npages-- > 0;
			     ptep += PAGE_SIZE / L2_S_SIZE) {
				l2pte_reset(ptep);
			}
			PTE_SYNC_RANGE(vtopte(va), size >> L2_S_SHIFT);
		}

		/* Clean out tlb. */
		cpu_tlb_flushID();

		args->nreturns = 1;
	} else if (strcmp(name, "translate") == 0) {
		vaddr_t va;
		paddr_t pa;
		int mode;
		pt_entry_t pte;

		/* Check format. */
		if (nargs != 1 || nreturns < 4) {
			args_n_results[nargs] = -1;
			args->nreturns = 1;
			return;
		}
		args_n_results[nargs] =	0;	/* properly formatted request */

		va = (vaddr_t)args_n_results[0];

		/* Sanity checks.
		 * For now, I am only willing to translate va's in the
		 * "ofw range." Eventually, I may be more generous. -JJK
		 */
		if ((va & PGOFSET) != 0 ||  va < OFW_VIRT_BASE ||
		    va >= (OFW_VIRT_BASE + OFW_VIRT_SIZE)) {
			args_n_results[nargs + 1] = -1;
			args->nreturns = 1;
			return;
		}

		/* Lookup mapping. */
		pte = *vtopte(va);
		if (pte == 0) {
			/* No mapping. */
			args_n_results[nargs + 1] = -1;
			args->nreturns = 2;
		} else {
			/* Existing mapping. */
			pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
			mode = (pte & 0x0C00) | (0 << 5) | (pte & 0x000C);	/* AP | DOM | CB */

			args_n_results[nargs + 1] = 0;
			args_n_results[nargs + 2] = pa;
			args_n_results[nargs + 3] =	mode;
			args->nreturns = 4;
		}
	} else if (strcmp(name, "claim-phys") == 0) {
		struct pglist alloclist;
		paddr_t low, high, align;
		psize_t size;

		/*
		 * XXX
		 * XXX THIS IS A GROSS HACK AND NEEDS TO BE REWRITTEN. -- cgd
		 * XXX
		 */

		/* Check format. */
		if (nargs != 4 || nreturns < 3) {
			args_n_results[nargs] = -1;
			args->nreturns = 1;
			return;
		}
		args_n_results[nargs] =	0;	/* properly formatted request */

		low = args_n_results[0];
		size = args_n_results[2];
		align = args_n_results[3];
		high = args_n_results[1] + size;

#if 0
		printf("claim-phys: low = 0x%x, size = 0x%x, align = 0x%x, high = 0x%x\n",
		    low, size, align, high);
		align = size;
		printf("forcing align to be 0x%x\n", align);
#endif

		args_n_results[nargs + 1] =
		uvm_pglistalloc(size, low, high, align, 0, &alloclist, 1, 0);
#if 0
		printf(" -> 0x%lx", args_n_results[nargs + 1]);
#endif
		if (args_n_results[nargs + 1] != 0) {
#if 0
			printf("(failed)\n");
#endif
			args_n_results[nargs + 1] = -1;
			args->nreturns = 2;
			return;
		} 
		args_n_results[nargs + 2] = VM_PAGE_TO_PHYS(alloclist.tqh_first);
#if 0
		printf("(succeeded: pa = 0x%lx)\n", args_n_results[nargs + 2]);
#endif
		args->nreturns = 3;

	} else if (strcmp(name, "release-phys") == 0) {
		printf("unimplemented ofw callback - %s\n", name);
		args_n_results[nargs] = -1;
		args->nreturns = 1;
	} else if (strcmp(name, "claim-virt") == 0) {
		vaddr_t va;
		vaddr_t align;

		/* XXX - notyet */
/*		printf("unimplemented ofw callback - %s\n", name);*/
		args_n_results[nargs] = -1;
		args->nreturns = 1;
		return;

		/* Check format. */
		if (nargs != 2 || nreturns < 3) {
		    args_n_results[nargs] = -1;
		    args->nreturns = 1;
		    return;
		}
		args_n_results[nargs] =	0;	/* properly formatted request */

		/* Allocate size bytes with specified alignment. */
		align = (vaddr_t)args_n_results[1];
		if (align % PAGE_SIZE != 0) {
			args_n_results[nargs + 1] = -1;
			args->nreturns = 2;
			return;
		}

		if (va == 0) {
			/* Couldn't allocate. */
			args_n_results[nargs + 1] = -1;
			args->nreturns = 2;
		} else {
			/* Successful allocation. */
			args_n_results[nargs + 1] = 0;
			args_n_results[nargs + 2] = va;
			args->nreturns = 3;
		}
	} else if (strcmp(name, "release-virt") == 0) {

		/* XXX - notyet */
		printf("unimplemented ofw callback - %s\n", name);
		args_n_results[nargs] = -1;
		args->nreturns = 1;
		return;

		/* Check format. */
		if (nargs != 2 || nreturns < 1) {
			args_n_results[nargs] = -1;
			args->nreturns = 1;
			return;
		}
		args_n_results[nargs] =	0;	/* properly formatted request */

		args->nreturns = 1;
	} else {
		args_n_results[nargs] = -1;
		args->nreturns = 1;
	}
}

static void
ofw_construct_proc0_addrspace(void)
{
	int i, oft;
	static pv_addr_t proc0_pt_sys;
	static pv_addr_t proc0_pt_kernel[KERNEL_IMG_PTS];
	static pv_addr_t proc0_pt_vmdata[KERNEL_VMDATA_PTS];
	static pv_addr_t proc0_pt_ofw[KERNEL_OFW_PTS];
	static pv_addr_t proc0_pt_io[KERNEL_IO_PTS];
	static pv_addr_t msgbuf;
	vaddr_t L1pagetable;
	struct mem_translation *tp;

	/* Set-up the system page. */
	KASSERT(vector_page == 0);	/* XXX for now */
	systempage.pv_va = ofw_claimvirt(vector_page, PAGE_SIZE, 0);
	if (systempage.pv_va == -1) {
		/* Something was already mapped to vector_page's VA. */
		systempage.pv_va = vector_page;
		systempage.pv_pa = ofw_gettranslation(vector_page);
		if (systempage.pv_pa == -1)
			panic("bogus result from gettranslation(vector_page)");
	} else {
		/* We were just allocated the page-length range at VA 0. */
		if (systempage.pv_va != vector_page)
			panic("bogus result from claimvirt(vector_page, PAGE_SIZE, 0)");

		/* Now allocate a physical page, and establish the mapping. */
		systempage.pv_pa = ofw_claimphys(0, PAGE_SIZE, PAGE_SIZE);
		if (systempage.pv_pa == -1)
			panic("bogus result from claimphys(0, PAGE_SIZE, PAGE_SIZE)");
		ofw_settranslation(systempage.pv_va, systempage.pv_pa,
		    PAGE_SIZE, -1);	/* XXX - mode? -JJK */

		/* Zero the memory. */
		memset((char *)systempage.pv_va, 0, PAGE_SIZE);
	}

	/* Allocate/initialize space for the proc0, NetBSD-managed */
	/* page tables that we will be switching to soon. */
	ofw_claimpages(&virt_freeptr, &kernel_l1pt, L1_TABLE_SIZE);
	ofw_claimpages(&virt_freeptr, &proc0_pt_sys, L2_TABLE_SIZE);
	for (i = 0; i < KERNEL_IMG_PTS; i++)
		ofw_claimpages(&virt_freeptr, &proc0_pt_kernel[i], L2_TABLE_SIZE);
	for (i = 0; i < KERNEL_VMDATA_PTS; i++)
		ofw_claimpages(&virt_freeptr, &proc0_pt_vmdata[i], L2_TABLE_SIZE);
	for (i = 0; i < KERNEL_OFW_PTS; i++)
		ofw_claimpages(&virt_freeptr, &proc0_pt_ofw[i], L2_TABLE_SIZE);
	for (i = 0; i < KERNEL_IO_PTS; i++)
		ofw_claimpages(&virt_freeptr, &proc0_pt_io[i], L2_TABLE_SIZE);

	/* Allocate/initialize space for stacks. */
#ifndef	OFWGENCFG
	ofw_claimpages(&virt_freeptr, &irqstack, PAGE_SIZE);
#endif
	ofw_claimpages(&virt_freeptr, &undstack, PAGE_SIZE);
	ofw_claimpages(&virt_freeptr, &abtstack, PAGE_SIZE);
	ofw_claimpages(&virt_freeptr, &kernelstack, UPAGES * PAGE_SIZE);

	/* Allocate/initialize space for msgbuf area. */
	ofw_claimpages(&virt_freeptr, &msgbuf, MSGBUFSIZE);
	msgbufphys = msgbuf.pv_pa;

	/* Construct the proc0 L1 pagetable. */
	L1pagetable = kernel_l1pt.pv_va;

	pmap_link_l2pt(L1pagetable, 0x0, &proc0_pt_sys);
	for (i = 0; i < KERNEL_IMG_PTS; i++)
		pmap_link_l2pt(L1pagetable, KERNEL_BASE + i * 0x00400000,
		    &proc0_pt_kernel[i]);
	for (i = 0; i < KERNEL_VMDATA_PTS; i++)
		pmap_link_l2pt(L1pagetable, KERNEL_VM_BASE + i * 0x00400000,
		    &proc0_pt_vmdata[i]);
	for (i = 0; i < KERNEL_OFW_PTS; i++)
		pmap_link_l2pt(L1pagetable, OFW_VIRT_BASE + i * 0x00400000,
		    &proc0_pt_ofw[i]);
	for (i = 0; i < KERNEL_IO_PTS; i++)
		pmap_link_l2pt(L1pagetable, IO_VIRT_BASE + i * 0x00400000,
		    &proc0_pt_io[i]);

	/*
	 * OK, we're done allocating.
	 * Get a dump of OFW's translations, and make the appropriate
	 * entries in the L2 pagetables that we just allocated.
	 */

	ofw_getvirttranslations();

	for (oft = 0,  tp = OFtranslations; oft < nOFtranslations;
	    oft++, tp++) {

		vaddr_t va;
		paddr_t pa;
		int npages = tp->size / PAGE_SIZE;

		/* Size must be an integral number of pages. */
		if (npages == 0 || tp->size % PAGE_SIZE != 0)
			panic("illegal ofw translation (size)");

		/* Make an entry for each page in the appropriate table. */
		for (va = tp->virt, pa = tp->phys; npages > 0;
		    va += PAGE_SIZE, pa += PAGE_SIZE, npages--) {
			/*
			 * Map the top bits to the appropriate L2 pagetable.
			 * The only allowable regions are page0, the
			 * kernel-static area, and the ofw area.
			 */
			switch (va >> (L1_S_SHIFT + 2)) {
			case 0:
				/* page0 */
				break;

#if KERNEL_IMG_PTS != 2
#error "Update ofw translation range list"
#endif
			case ( KERNEL_BASE                 >> (L1_S_SHIFT + 2)):
			case ((KERNEL_BASE   + 0x00400000) >> (L1_S_SHIFT + 2)):
				/* kernel static area */
				break;

			case ( OFW_VIRT_BASE               >> (L1_S_SHIFT + 2)):
			case ((OFW_VIRT_BASE + 0x00400000) >> (L1_S_SHIFT + 2)):
			case ((OFW_VIRT_BASE + 0x00800000) >> (L1_S_SHIFT + 2)):
			case ((OFW_VIRT_BASE + 0x00C00000) >> (L1_S_SHIFT + 2)):
				/* ofw area */
				break;

			case ( IO_VIRT_BASE               >> (L1_S_SHIFT + 2)):
			case ((IO_VIRT_BASE + 0x00400000) >> (L1_S_SHIFT + 2)):
			case ((IO_VIRT_BASE + 0x00800000) >> (L1_S_SHIFT + 2)):
			case ((IO_VIRT_BASE + 0x00C00000) >> (L1_S_SHIFT + 2)):
				/* io area */
				break;

			default:
				/* illegal */
				panic("illegal ofw translation (addr) %#lx",
				    va);
			}

			/* Make the entry. */
			pmap_map_entry(L1pagetable, va, pa,
			    VM_PROT_READ|VM_PROT_WRITE,
			    (tp->mode & 0xC) == 0xC ? PTE_CACHE
						    : PTE_NOCACHE);
		}
	}

	/*
	 * We don't actually want some of the mappings that we just
	 * set up to appear in proc0's address space.  In particular,
	 * we don't want aliases to physical addresses that the kernel
	 * has-mapped/will-map elsewhere.
	 */
	ofw_discardmappings(proc0_pt_kernel[KERNEL_IMG_PTS - 1].pv_va,
	    msgbuf.pv_va, MSGBUFSIZE);

	/* update the top of the kernel VM */
	pmap_curmaxkvaddr =
	    KERNEL_VM_BASE + (KERNEL_VMDATA_PTS * 0x00400000);
	
	/* 
         * gross hack for the sake of not thrashing the TLB and making
	 * cache flush more efficient: blast l1 ptes for sections.
         */
	for (oft = 0, tp = OFtranslations; oft < nOFtranslations; oft++, tp++) {
		vaddr_t va = tp->virt;
		paddr_t pa = tp->phys;

		if (((va | pa) & L1_S_OFFSET) == 0) {
			int nsections = tp->size / L1_S_SIZE;

			while (nsections--) {
				/* XXXJRT prot?? */
				pmap_map_section(L1pagetable, va, pa,
				    VM_PROT_READ|VM_PROT_WRITE,
				    (tp->mode & 0xC) == 0xC ? PTE_CACHE
							    : PTE_NOCACHE);
				va += L1_S_SIZE;
				pa += L1_S_SIZE;
			}
		}
	}
}


static void
ofw_getphysmeminfo(void)
{
	int phandle;
	int mem_len;
	int avail_len;
	int i;

	if ((phandle = OF_finddevice("/memory")) == -1 ||
	    (mem_len = OF_getproplen(phandle, "reg")) <= 0 ||
	    (OFphysmem = (struct mem_region *)ofw_malloc(mem_len)) == 0 ||
	    OF_getprop(phandle, "reg", OFphysmem, mem_len) != mem_len ||
	    (avail_len = OF_getproplen(phandle, "available")) <= 0 ||
 	    (OFphysavail = (struct mem_region *)ofw_malloc(avail_len)) == 0 ||
	    OF_getprop(phandle, "available", OFphysavail, avail_len)
	    != avail_len)
		panic("can't get physmeminfo from OFW");

	nOFphysmem = mem_len / sizeof(struct mem_region);
	nOFphysavail = avail_len / sizeof(struct mem_region);

	/*
	 * Sort the blocks in each array into ascending address order.
	 * Also, page-align all blocks.
	 */
	for (i = 0; i < 2; i++) {
		struct mem_region *tmp = (i == 0) ? OFphysmem : OFphysavail;
		struct mem_region *mp;
		int cnt =  (i == 0) ? nOFphysmem : nOFphysavail;
		int j;

#ifdef	OLDPRINTFS
		printf("ofw_getphysmeminfo:  %d blocks\n", cnt);
#endif

		/* XXX - Convert all the values to host order. -JJK */
		for (j = 0, mp = tmp; j < cnt; j++, mp++) {
			mp->start = of_decode_int((unsigned char *)&mp->start);
			mp->size = of_decode_int((unsigned char *)&mp->size);
		}

		for (j = 0, mp = tmp; j < cnt; j++, mp++) {
			u_int s, sz;
			struct mem_region *mp1;

			/* Page-align start of the block. */
			s = mp->start % PAGE_SIZE;
			if (s != 0) {
				s = (PAGE_SIZE - s);

				if (mp->size >= s) {
					mp->start += s;
					mp->size -= s;
				}
			}

			/* Page-align the size. */
			mp->size -= mp->size % PAGE_SIZE;

			/* Handle empty block. */
			if (mp->size == 0) {
				memmove(mp, mp + 1, (cnt - (mp - tmp))
				    * sizeof(struct mem_region));
				cnt--;
				mp--;
				continue;
			}

			/* Bubble sort. */
			s = mp->start;
			sz = mp->size;
			for (mp1 = tmp; mp1 < mp; mp1++)
				if (s < mp1->start)
					break;
			if (mp1 < mp) {
				memmove(mp1 + 1, mp1, (char *)mp - (char *)mp1);
				mp1->start = s;
				mp1->size = sz;
			}
		}

#ifdef	OLDPRINTFS
		for (mp = tmp; mp->size; mp++) {
			printf("%x, %x\n", mp->start, mp->size);
		}
#endif
	}
}


static void
ofw_getvirttranslations(void)
{
	int mmu_phandle;
	int mmu_ihandle;
	int trans_len;
	int over, len;
	int i;
	struct mem_translation *tp;

	mmu_ihandle = ofw_mmu_ihandle();

	/* overallocate to avoid increases during allocation */
	over = 4 * sizeof(struct mem_translation);
	if ((mmu_phandle = OF_instance_to_package(mmu_ihandle)) == -1 ||
	    (len = OF_getproplen(mmu_phandle, "translations")) <= 0 ||
	    (OFtranslations = ofw_malloc(len + over)) == 0 ||
	    (trans_len = OF_getprop(mmu_phandle, "translations",
	    OFtranslations, len + over)) > (len + over))
		panic("can't get virttranslations from OFW");

	/* XXX - Convert all the values to host order. -JJK */
	nOFtranslations = trans_len / sizeof(struct mem_translation);
#ifdef	OLDPRINTFS
	printf("ofw_getvirtmeminfo:  %d blocks\n", nOFtranslations);
#endif
	for (i = 0, tp = OFtranslations; i < nOFtranslations; i++, tp++) {
		tp->virt = of_decode_int((unsigned char *)&tp->virt);
		tp->size = of_decode_int((unsigned char *)&tp->size);
		tp->phys = of_decode_int((unsigned char *)&tp->phys);
		tp->mode = of_decode_int((unsigned char *)&tp->mode);
	}
}

/*
 * ofw_valloc: allocate blocks of VM for IO and other special purposes
 */
typedef struct _vfree {
	struct _vfree *pNext;
	vaddr_t start;
	vsize_t size;
} VFREE, *PVFREE;

static VFREE vfinitial = { NULL, IO_VIRT_BASE, IO_VIRT_SIZE };

static PVFREE vflist = &vfinitial;

static vaddr_t
ofw_valloc(vsize_t size, vaddr_t align)
{
	PVFREE        *ppvf;
	PVFREE        pNew;
	vaddr_t       new;
	vaddr_t       lead;

	for (ppvf = &vflist; *ppvf; ppvf = &((*ppvf)->pNext)) {
		if (align == 0) {
			new = (*ppvf)->start;
			lead = 0;
		} else {
			new  = ((*ppvf)->start + (align - 1)) & ~(align - 1);
			lead = new - (*ppvf)->start;
		}

		if (((*ppvf)->size - lead) >= size) {
 			if (lead == 0) {
				/* using whole block */
				if (size == (*ppvf)->size) { 
					/* splice out of list */
					(*ppvf) = (*ppvf)->pNext;
				} else { /* tail of block is free */
					(*ppvf)->start = new + size;
					(*ppvf)->size -= size;
				}
			} else {
				vsize_t tail = ((*ppvf)->start
				    + (*ppvf)->size) - (new + size);
				/* free space at beginning */
				(*ppvf)->size = lead;

				if (tail != 0) {
					/* free space at tail */
					pNew = ofw_malloc(sizeof(VFREE));
					pNew->pNext  = (*ppvf)->pNext;
					(*ppvf)->pNext = pNew;
					pNew->start  = new + size;
					pNew->size   = tail;
				}
			}
			return new;
		} /* END if */
	} /* END for */

	return -1;
}

vaddr_t
ofw_map(paddr_t pa, vsize_t size, int cb_bits)
{
	vaddr_t va;

	if ((va = ofw_valloc(size, size)) == -1)
		panic("cannot alloc virtual memory for %#lx", pa);

	ofw_claimvirt(va, size, 0); /* make sure OFW knows about the memory */

	ofw_settranslation(va, pa, size, L2_AP(AP_KRW) | cb_bits);

	return va;
}

static int
ofw_mem_ihandle(void)
{
	static int mem_ihandle = 0;
	int chosen;

	if (mem_ihandle != 0)
		return(mem_ihandle);

	if ((chosen = OF_finddevice("/chosen")) == -1 ||
	    OF_getprop(chosen, "memory", &mem_ihandle, sizeof(int)) < 0)
		panic("ofw_mem_ihandle");

	mem_ihandle = of_decode_int((unsigned char *)&mem_ihandle);

	return(mem_ihandle);
}


static int
ofw_mmu_ihandle(void)
{
	static int mmu_ihandle = 0;
	int chosen;

	if (mmu_ihandle != 0)
		return(mmu_ihandle);

	if ((chosen = OF_finddevice("/chosen")) == -1 ||
	    OF_getprop(chosen, "mmu", &mmu_ihandle, sizeof(int)) < 0)
		panic("ofw_mmu_ihandle");

	mmu_ihandle = of_decode_int((unsigned char *)&mmu_ihandle);

	return(mmu_ihandle);
}


/* Return -1 on failure. */
static paddr_t
ofw_claimphys(paddr_t pa, psize_t size, paddr_t align)
{
	int mem_ihandle = ofw_mem_ihandle();

/*	printf("ofw_claimphys (%x, %x, %x) --> ", pa, size, align);*/
	if (align == 0) {
		/* Allocate at specified base; alignment is ignored. */
		pa = OF_call_method_1("claim", mem_ihandle, 3, pa, size, align);
	} else {
		/* Allocate anywhere, with specified alignment. */
		pa = OF_call_method_1("claim", mem_ihandle, 2, size, align);
	}

/*	printf("%x\n", pa);*/
	return(pa);
}


#if 0
/* Return -1 on failure. */
static paddr_t
ofw_releasephys(paddr_t pa, psize_t size)
{
	int mem_ihandle = ofw_mem_ihandle();

/*	printf("ofw_releasephys (%x, %x)\n", pa, size);*/

	return (OF_call_method_1("release", mem_ihandle, 2, pa, size));
}
#endif

/* Return -1 on failure. */
static vaddr_t
ofw_claimvirt(vaddr_t va, vsize_t size, vaddr_t align)
{
	int mmu_ihandle = ofw_mmu_ihandle();

	/*printf("ofw_claimvirt (%x, %x, %x) --> ", va, size, align);*/
	if (align == 0) {
		/* Allocate at specified base; alignment is ignored. */
		va = OF_call_method_1("claim", mmu_ihandle, 3, va, size, align);
	} else {
		/* Allocate anywhere, with specified alignment. */
		va = OF_call_method_1("claim", mmu_ihandle, 2, size, align);
	}

	/*printf("%x\n", va);*/
	return(va);
}

/* Return -1 if no mapping. */
paddr_t
ofw_gettranslation(vaddr_t va)
{
	int mmu_ihandle = ofw_mmu_ihandle();
	paddr_t pa;
	int mode;
	int exists;

#ifdef OFW_DEBUG
	printf("ofw_gettranslation (%x) --> ", (uint32_t)va);
#endif
	exists = 0;	    /* gets set to true if translation exists */
	if (OF_call_method("translate", mmu_ihandle, 1, 3, va, &pa, &mode,
	    &exists) != 0) {
#ifdef OFW_DEBUG
		printf("(failed)\n");
#endif
		return(-1);
	}

#ifdef OFW_DEBUG
	printf("%d %x\n", exists, (uint32_t)pa);
#endif
	return(exists ? pa : -1);
}


static void
ofw_settranslation(vaddr_t va, paddr_t pa, vsize_t size, int mode)
{
	int mmu_ihandle = ofw_mmu_ihandle();

#ifdef OFW_DEBUG
	printf("ofw_settranslation (%x, %x, %x, %x) --> void\n", (uint32_t)va,
	    (uint32_t)pa, (uint32_t)size, (uint32_t)mode);
#endif
	if (OF_call_method("map", mmu_ihandle, 4, 0, pa, va, size, mode) != 0)
		panic("ofw_settranslation failed");
}

/*
 *  Allocation routine used before the kernel takes over memory.
 *  Use this for efficient storage for things that aren't rounded to
 *  page size.
 *
 *  The point here is not necessarily to be very efficient (even though
 *  that's sort of nice), but to do proper dynamic allocation to avoid
 *  size-limitation errors.
 *
 */

typedef struct _leftover {
	struct _leftover *pNext;
	vsize_t size;
} LEFTOVER, *PLEFTOVER;

/* leftover bits of pages.  first word is pointer to next.
   second word is size of leftover */
static PLEFTOVER leftovers = NULL;

static void *
ofw_malloc(vsize_t size)
{
	PLEFTOVER   *ppLeftover;
	PLEFTOVER   pLeft;
	pv_addr_t   new;
	vsize_t   newSize, claim_size;

	/* round and set minimum size */
	size = uimax(sizeof(LEFTOVER), 
	    ((size + (sizeof(LEFTOVER) - 1)) & ~(sizeof(LEFTOVER) - 1)));

	for (ppLeftover = &leftovers; *ppLeftover;
	    ppLeftover = &((*ppLeftover)->pNext))
		if ((*ppLeftover)->size >= size)
			break;

	if (*ppLeftover) { /* have a leftover of the right size */
		/* remember the leftover */
		new.pv_va = (vaddr_t)*ppLeftover;
		if ((*ppLeftover)->size < (size + sizeof(LEFTOVER))) {
			/* splice out of chain */
			*ppLeftover = (*ppLeftover)->pNext;
		} else {
			/* remember the next pointer */
			pLeft = (*ppLeftover)->pNext;
			newSize = (*ppLeftover)->size - size; /* reduce size */
			/* move pointer */
			*ppLeftover = (PLEFTOVER)(((vaddr_t)*ppLeftover)
			    + size);
			(*ppLeftover)->pNext = pLeft;
			(*ppLeftover)->size  = newSize;
		}
	} else {
		claim_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
		ofw_claimpages(&virt_freeptr, &new, claim_size);
		if ((size + sizeof(LEFTOVER)) <= claim_size) {
			pLeft = (PLEFTOVER)(new.pv_va + size);
			pLeft->pNext = leftovers;
			pLeft->size = claim_size - size;
			leftovers = pLeft;
		}
	}

	return (void *)(new.pv_va);
}

/*
 *  Here is a really, really sleazy free.  It's not used right now,
 *  because it's not worth the extra complexity for just a few bytes.
 *
 */
#if 0
static void
ofw_free(vaddr_t addr, vsize_t size)
{
	PLEFTOVER pLeftover = (PLEFTOVER)addr;

	/* splice right into list without checks or compaction */
	pLeftover->pNext = leftovers;
	pLeftover->size  = size;
	leftovers        = pLeftover;
}
#endif

/*
 *  Allocate and zero round(size)/PAGE_SIZE pages of memory.
 *  We guarantee that the allocated memory will be
 *  aligned to a boundary equal to the smallest power of
 *  2 greater than or equal to size.
 *  free_pp is an IN/OUT parameter which points to the
 *  last allocated virtual address in an allocate-downwards
 *  stack.  pv_p is an OUT parameter which contains the
 *  virtual and physical base addresses of the allocated
 *  memory.
 */
static void
ofw_claimpages(vaddr_t *free_pp, pv_addr_t *pv_p, vsize_t size)
{
	/* round-up to page boundary */
	vsize_t alloc_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
	vsize_t aligned_size;
	vaddr_t va;
	paddr_t pa;

	if (alloc_size == 0)
		panic("ofw_claimpages zero");

	for (aligned_size = 1; aligned_size < alloc_size; aligned_size <<= 1)
		;

	/*  The only way to provide the alignment guarantees is to
	 *  allocate the virtual and physical ranges separately,
	 *  then do an explicit map call.
	 */
	va = (*free_pp & ~(aligned_size - 1)) - aligned_size;
	if (ofw_claimvirt(va, alloc_size, 0) != va)
		panic("ofw_claimpages va alloc");
	pa = ofw_claimphys(0, alloc_size, aligned_size);
	if (pa == -1)
		panic("ofw_claimpages pa alloc");
	/* XXX - what mode? -JJK */
	ofw_settranslation(va, pa, alloc_size, -1);

	/* The memory's mapped-in now, so we can zero it. */
	memset((char *)va, 0, alloc_size);

	/* Set OUT parameters. */
	*free_pp = va;
	pv_p->pv_va = va;
	pv_p->pv_pa = pa;
}


static void
ofw_discardmappings(vaddr_t L2pagetable, vaddr_t va, vsize_t size)
{
	/* round-up to page boundary */
	vsize_t alloc_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
	int npages = alloc_size / PAGE_SIZE;

	if (npages == 0)
		panic("ofw_discardmappings zero");

	/* Discard each mapping. */
	for (; npages > 0; va += PAGE_SIZE, npages--) {
		/* Sanity. The current entry should be non-null. */
		if (ReadWord(L2pagetable + ((va >> 10) & 0x00000FFC)) == 0)
			panic("ofw_discardmappings zero entry");

		/* Clear the entry. */
		WriteWord(L2pagetable + ((va >> 10) & 0x00000FFC), 0);
	}
}


static void
ofw_initallocator(void)
{
    
}

#if (NIGSFB_OFBUS > 0) || (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
static void
reset_screen(void)
{

	if ((console_ihandle == 0) || (console_ihandle == -1))
		return;

	OF_call_method("install", console_ihandle, 0, 0);
}
#endif /* (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0) */