1/*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright 2003-2011 Netlogic Microsystems (Netlogic). All rights 5 * reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are 9 * met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY Netlogic Microsystems ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NETLOGIC OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 * THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * NETLOGIC_BSD */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD$"); 34 35#include <sys/param.h> 36#include <sys/systm.h> 37#include <sys/bus.h> 38#include <sys/kernel.h> 39#include <sys/endian.h> 40#include <sys/malloc.h> 41#include <sys/ktr.h> 42 43#include <vm/vm.h> 44#include <vm/pmap.h> 45#include <vm/vm_kern.h> 46#include <vm/vm_extern.h> 47 48#include <machine/bus.h> 49#include <machine/cache.h> 50 51static int 52rmi_pci_bus_space_map(void *t, bus_addr_t addr, 53 bus_size_t size, int flags, 54 bus_space_handle_t * bshp); 55 56static void 57rmi_pci_bus_space_unmap(void *t, bus_space_handle_t bsh, 58 bus_size_t size); 59 60static int 61rmi_pci_bus_space_subregion(void *t, 62 bus_space_handle_t bsh, 63 bus_size_t offset, bus_size_t size, 64 bus_space_handle_t * nbshp); 65 66static u_int8_t 67rmi_pci_bus_space_read_1(void *t, 68 bus_space_handle_t handle, 69 bus_size_t offset); 70 71static u_int16_t 72rmi_pci_bus_space_read_2(void *t, 73 bus_space_handle_t handle, 74 bus_size_t offset); 75 76static u_int32_t 77rmi_pci_bus_space_read_4(void *t, 78 bus_space_handle_t handle, 79 bus_size_t offset); 80 81static void 82rmi_pci_bus_space_read_multi_1(void *t, 83 bus_space_handle_t handle, 84 bus_size_t offset, u_int8_t * addr, 85 size_t count); 86 87static void 88rmi_pci_bus_space_read_multi_2(void *t, 89 bus_space_handle_t handle, 90 bus_size_t offset, u_int16_t * addr, 91 size_t count); 92 93static void 94rmi_pci_bus_space_read_multi_4(void *t, 95 bus_space_handle_t handle, 96 bus_size_t offset, u_int32_t * addr, 97 size_t count); 98 99static void 100rmi_pci_bus_space_read_region_1(void *t, 101 bus_space_handle_t bsh, 102 bus_size_t offset, u_int8_t * addr, 103 size_t count); 104 105static void 106rmi_pci_bus_space_read_region_2(void *t, 107 bus_space_handle_t bsh, 108 bus_size_t offset, u_int16_t * addr, 109 size_t count); 110 111static void 112rmi_pci_bus_space_read_region_4(void *t, 113 bus_space_handle_t bsh, 114 bus_size_t offset, u_int32_t * addr, 115 size_t count); 116 117static void 118rmi_pci_bus_space_write_1(void *t, 119 bus_space_handle_t handle, 120 bus_size_t offset, u_int8_t value); 121 122static void 123rmi_pci_bus_space_write_2(void *t, 124 bus_space_handle_t handle, 125 bus_size_t offset, u_int16_t value); 126 127static void 128rmi_pci_bus_space_write_4(void *t, 129 bus_space_handle_t handle, 130 bus_size_t offset, u_int32_t value); 131 132static void 133rmi_pci_bus_space_write_multi_1(void *t, 134 bus_space_handle_t handle, 135 bus_size_t offset, 136 const u_int8_t * addr, 137 size_t count); 138 139static void 140rmi_pci_bus_space_write_multi_2(void *t, 141 bus_space_handle_t handle, 142 bus_size_t offset, 143 const u_int16_t * addr, 144 size_t count); 145 146static void 147rmi_pci_bus_space_write_multi_4(void *t, 148 bus_space_handle_t handle, 149 bus_size_t offset, 150 const u_int32_t * addr, 151 size_t count); 152 153static void 154rmi_pci_bus_space_write_region_2(void *t, 155 bus_space_handle_t bsh, 156 bus_size_t offset, 157 const u_int16_t * addr, 158 size_t count); 159 160static void 161rmi_pci_bus_space_write_region_4(void *t, 162 bus_space_handle_t bsh, 163 bus_size_t offset, 164 const u_int32_t * addr, 165 size_t count); 166 167static void 168rmi_pci_bus_space_set_region_2(void *t, 169 bus_space_handle_t bsh, 170 bus_size_t offset, u_int16_t value, 171 size_t count); 172 173static void 174rmi_pci_bus_space_set_region_4(void *t, 175 bus_space_handle_t bsh, 176 bus_size_t offset, u_int32_t value, 177 size_t count); 178 179static void 180rmi_pci_bus_space_barrier(void *tag __unused, bus_space_handle_t bsh __unused, 181 bus_size_t offset __unused, bus_size_t len __unused, int flags); 182 183static void 184rmi_pci_bus_space_copy_region_2(void *t, 185 bus_space_handle_t bsh1, 186 bus_size_t off1, 187 bus_space_handle_t bsh2, 188 bus_size_t off2, size_t count); 189 190u_int8_t 191rmi_pci_bus_space_read_stream_1(void *t, bus_space_handle_t handle, 192 bus_size_t offset); 193 194static u_int16_t 195rmi_pci_bus_space_read_stream_2(void *t, bus_space_handle_t handle, 196 bus_size_t offset); 197 198static u_int32_t 199rmi_pci_bus_space_read_stream_4(void *t, bus_space_handle_t handle, 200 bus_size_t offset); 201 202static void 203rmi_pci_bus_space_read_multi_stream_1(void *t, 204 bus_space_handle_t handle, 205 bus_size_t offset, u_int8_t * addr, 206 size_t count); 207 208static void 209rmi_pci_bus_space_read_multi_stream_2(void *t, 210 bus_space_handle_t handle, 211 bus_size_t offset, u_int16_t * addr, 212 size_t count); 213 214static void 215rmi_pci_bus_space_read_multi_stream_4(void *t, 216 bus_space_handle_t handle, 217 bus_size_t offset, u_int32_t * addr, 218 size_t count); 219 220void 221rmi_pci_bus_space_write_stream_1(void *t, bus_space_handle_t bsh, 222 bus_size_t offset, u_int8_t value); 223 224static void 225rmi_pci_bus_space_write_stream_2(void *t, bus_space_handle_t handle, 226 bus_size_t offset, u_int16_t value); 227 228static void 229rmi_pci_bus_space_write_stream_4(void *t, bus_space_handle_t handle, 230 bus_size_t offset, u_int32_t value); 231 232static void 233rmi_pci_bus_space_write_multi_stream_1(void *t, 234 bus_space_handle_t handle, 235 bus_size_t offset, 236 const u_int8_t * addr, 237 size_t count); 238 239static void 240rmi_pci_bus_space_write_multi_stream_2(void *t, 241 bus_space_handle_t handle, 242 bus_size_t offset, 243 const u_int16_t * addr, 244 size_t count); 245 246static void 247rmi_pci_bus_space_write_multi_stream_4(void *t, 248 bus_space_handle_t handle, 249 bus_size_t offset, 250 const u_int32_t * addr, 251 size_t count); 252 253#define TODO() printf("XLR memory bus space function '%s' unimplemented\n", __func__) 254 255static struct bus_space local_rmi_pci_bus_space = { 256 /* cookie */ 257 (void *)0, 258 259 /* mapping/unmapping */ 260 rmi_pci_bus_space_map, 261 rmi_pci_bus_space_unmap, 262 rmi_pci_bus_space_subregion, 263 264 /* allocation/deallocation */ 265 NULL, 266 NULL, 267 268 /* barrier */ 269 rmi_pci_bus_space_barrier, 270 271 /* read (single) */ 272 rmi_pci_bus_space_read_1, 273 rmi_pci_bus_space_read_2, 274 rmi_pci_bus_space_read_4, 275 NULL, 276 277 /* read multiple */ 278 rmi_pci_bus_space_read_multi_1, 279 rmi_pci_bus_space_read_multi_2, 280 rmi_pci_bus_space_read_multi_4, 281 NULL, 282 283 /* read region */ 284 rmi_pci_bus_space_read_region_1, 285 rmi_pci_bus_space_read_region_2, 286 rmi_pci_bus_space_read_region_4, 287 NULL, 288 289 /* write (single) */ 290 rmi_pci_bus_space_write_1, 291 rmi_pci_bus_space_write_2, 292 rmi_pci_bus_space_write_4, 293 NULL, 294 295 /* write multiple */ 296 rmi_pci_bus_space_write_multi_1, 297 rmi_pci_bus_space_write_multi_2, 298 rmi_pci_bus_space_write_multi_4, 299 NULL, 300 301 /* write region */ 302 NULL, 303 rmi_pci_bus_space_write_region_2, 304 rmi_pci_bus_space_write_region_4, 305 NULL, 306 307 /* set multiple */ 308 NULL, 309 NULL, 310 NULL, 311 NULL, 312 313 /* set region */ 314 NULL, 315 rmi_pci_bus_space_set_region_2, 316 rmi_pci_bus_space_set_region_4, 317 NULL, 318 319 /* copy */ 320 NULL, 321 rmi_pci_bus_space_copy_region_2, 322 NULL, 323 NULL, 324 325 /* read (single) stream */ 326 rmi_pci_bus_space_read_stream_1, 327 rmi_pci_bus_space_read_stream_2, 328 rmi_pci_bus_space_read_stream_4, 329 NULL, 330 331 /* read multiple stream */ 332 rmi_pci_bus_space_read_multi_stream_1, 333 rmi_pci_bus_space_read_multi_stream_2, 334 rmi_pci_bus_space_read_multi_stream_4, 335 NULL, 336 337 /* read region stream */ 338 rmi_pci_bus_space_read_region_1, 339 rmi_pci_bus_space_read_region_2, 340 rmi_pci_bus_space_read_region_4, 341 NULL, 342 343 /* write (single) stream */ 344 rmi_pci_bus_space_write_stream_1, 345 rmi_pci_bus_space_write_stream_2, 346 rmi_pci_bus_space_write_stream_4, 347 NULL, 348 349 /* write multiple stream */ 350 rmi_pci_bus_space_write_multi_stream_1, 351 rmi_pci_bus_space_write_multi_stream_2, 352 rmi_pci_bus_space_write_multi_stream_4, 353 NULL, 354 355 /* write region stream */ 356 NULL, 357 rmi_pci_bus_space_write_region_2, 358 rmi_pci_bus_space_write_region_4, 359 NULL, 360}; 361 362/* generic bus_space tag */ 363bus_space_tag_t rmi_pci_bus_space = &local_rmi_pci_bus_space; 364 365/* 366 * Map a region of device bus space into CPU virtual address space. 367 */ 368static int 369rmi_pci_bus_space_map(void *t __unused, bus_addr_t addr, 370 bus_size_t size __unused, int flags __unused, 371 bus_space_handle_t * bshp) 372{ 373 *bshp = addr; 374 return (0); 375} 376 377/* 378 * Unmap a region of device bus space. 379 */ 380static void 381rmi_pci_bus_space_unmap(void *t __unused, bus_space_handle_t bsh __unused, 382 bus_size_t size __unused) 383{ 384} 385 386/* 387 * Get a new handle for a subregion of an already-mapped area of bus space. 388 */ 389 390static int 391rmi_pci_bus_space_subregion(void *t __unused, bus_space_handle_t bsh, 392 bus_size_t offset, bus_size_t size __unused, 393 bus_space_handle_t * nbshp) 394{ 395 *nbshp = bsh + offset; 396 return (0); 397} 398 399/* 400 * Read a 1, 2, 4, or 8 byte quantity from bus space 401 * described by tag/handle/offset. 402 */ 403 404static u_int8_t 405rmi_pci_bus_space_read_1(void *tag, bus_space_handle_t handle, 406 bus_size_t offset) 407{ 408 return (u_int8_t) (*(volatile u_int8_t *)(handle + offset)); 409} 410 411static u_int16_t 412rmi_pci_bus_space_read_2(void *tag, bus_space_handle_t handle, 413 bus_size_t offset) 414{ 415 u_int16_t value; 416 417 value = *(volatile u_int16_t *)(handle + offset); 418 return bswap16(value); 419} 420 421static u_int32_t 422rmi_pci_bus_space_read_4(void *tag, bus_space_handle_t handle, 423 bus_size_t offset) 424{ 425 uint32_t value; 426 427 value = *(volatile u_int32_t *)(handle + offset); 428 return bswap32(value); 429} 430 431/* 432 * Read `count' 1, 2, 4, or 8 byte quantities from bus space 433 * described by tag/handle/offset and copy into buffer provided. 434 */ 435static void 436rmi_pci_bus_space_read_multi_1(void *tag, bus_space_handle_t handle, 437 bus_size_t offset, u_int8_t * addr, size_t count) 438{ 439 while (count--) { 440 *addr = *(volatile u_int8_t *)(handle + offset); 441 addr++; 442 } 443} 444 445static void 446rmi_pci_bus_space_read_multi_2(void *tag, bus_space_handle_t handle, 447 bus_size_t offset, u_int16_t * addr, size_t count) 448{ 449 450 while (count--) { 451 *addr = *(volatile u_int16_t *)(handle + offset); 452 *addr = bswap16(*addr); 453 addr++; 454 } 455} 456 457static void 458rmi_pci_bus_space_read_multi_4(void *tag, bus_space_handle_t handle, 459 bus_size_t offset, u_int32_t * addr, size_t count) 460{ 461 462 while (count--) { 463 *addr = *(volatile u_int32_t *)(handle + offset); 464 *addr = bswap32(*addr); 465 addr++; 466 } 467} 468 469/* 470 * Write the 1, 2, 4, or 8 byte value `value' to bus space 471 * described by tag/handle/offset. 472 */ 473 474static void 475rmi_pci_bus_space_write_1(void *tag, bus_space_handle_t handle, 476 bus_size_t offset, u_int8_t value) 477{ 478 mips_sync(); 479 *(volatile u_int8_t *)(handle + offset) = value; 480} 481 482static void 483rmi_pci_bus_space_write_2(void *tag, bus_space_handle_t handle, 484 bus_size_t offset, u_int16_t value) 485{ 486 mips_sync(); 487 *(volatile u_int16_t *)(handle + offset) = bswap16(value); 488} 489 490 491static void 492rmi_pci_bus_space_write_4(void *tag, bus_space_handle_t handle, 493 bus_size_t offset, u_int32_t value) 494{ 495 mips_sync(); 496 *(volatile u_int32_t *)(handle + offset) = bswap32(value); 497} 498 499/* 500 * Write `count' 1, 2, 4, or 8 byte quantities from the buffer 501 * provided to bus space described by tag/handle/offset. 502 */ 503 504 505static void 506rmi_pci_bus_space_write_multi_1(void *tag, bus_space_handle_t handle, 507 bus_size_t offset, const u_int8_t * addr, size_t count) 508{ 509 mips_sync(); 510 while (count--) { 511 (*(volatile u_int8_t *)(handle + offset)) = *addr; 512 addr++; 513 } 514} 515 516static void 517rmi_pci_bus_space_write_multi_2(void *tag, bus_space_handle_t handle, 518 bus_size_t offset, const u_int16_t * addr, size_t count) 519{ 520 mips_sync(); 521 while (count--) { 522 (*(volatile u_int16_t *)(handle + offset)) = bswap16(*addr); 523 addr++; 524 } 525} 526 527static void 528rmi_pci_bus_space_write_multi_4(void *tag, bus_space_handle_t handle, 529 bus_size_t offset, const u_int32_t * addr, size_t count) 530{ 531 mips_sync(); 532 while (count--) { 533 (*(volatile u_int32_t *)(handle + offset)) = bswap32(*addr); 534 addr++; 535 } 536} 537 538/* 539 * Write `count' 1, 2, 4, or 8 byte value `val' to bus space described 540 * by tag/handle starting at `offset'. 541 */ 542 543static void 544rmi_pci_bus_space_set_region_2(void *t, bus_space_handle_t bsh, 545 bus_size_t offset, u_int16_t value, size_t count) 546{ 547 bus_addr_t addr = bsh + offset; 548 549 for (; count != 0; count--, addr += 2) 550 (*(volatile u_int16_t *)(addr)) = value; 551} 552 553static void 554rmi_pci_bus_space_set_region_4(void *t, bus_space_handle_t bsh, 555 bus_size_t offset, u_int32_t value, size_t count) 556{ 557 bus_addr_t addr = bsh + offset; 558 559 for (; count != 0; count--, addr += 4) 560 (*(volatile u_int32_t *)(addr)) = value; 561} 562 563 564/* 565 * Copy `count' 1, 2, 4, or 8 byte values from bus space starting 566 * at tag/bsh1/off1 to bus space starting at tag/bsh2/off2. 567 */ 568static void 569rmi_pci_bus_space_copy_region_2(void *t, bus_space_handle_t bsh1, 570 bus_size_t off1, bus_space_handle_t bsh2, 571 bus_size_t off2, size_t count) 572{ 573 TODO(); 574} 575 576/* 577 * Read `count' 1, 2, 4, or 8 byte quantities from bus space 578 * described by tag/handle/offset and copy into buffer provided. 579 */ 580 581u_int8_t 582rmi_pci_bus_space_read_stream_1(void *t, bus_space_handle_t handle, 583 bus_size_t offset) 584{ 585 586 return *((volatile u_int8_t *)(handle + offset)); 587} 588 589 590static u_int16_t 591rmi_pci_bus_space_read_stream_2(void *t, bus_space_handle_t handle, 592 bus_size_t offset) 593{ 594 return *(volatile u_int16_t *)(handle + offset); 595} 596 597 598static u_int32_t 599rmi_pci_bus_space_read_stream_4(void *t, bus_space_handle_t handle, 600 bus_size_t offset) 601{ 602 return (*(volatile u_int32_t *)(handle + offset)); 603} 604 605 606static void 607rmi_pci_bus_space_read_multi_stream_1(void *tag, bus_space_handle_t handle, 608 bus_size_t offset, u_int8_t * addr, size_t count) 609{ 610 while (count--) { 611 *addr = (*(volatile u_int8_t *)(handle + offset)); 612 addr++; 613 } 614} 615 616static void 617rmi_pci_bus_space_read_multi_stream_2(void *tag, bus_space_handle_t handle, 618 bus_size_t offset, u_int16_t * addr, size_t count) 619{ 620 while (count--) { 621 *addr = (*(volatile u_int16_t *)(handle + offset)); 622 addr++; 623 } 624} 625 626static void 627rmi_pci_bus_space_read_multi_stream_4(void *tag, bus_space_handle_t handle, 628 bus_size_t offset, u_int32_t * addr, size_t count) 629{ 630 while (count--) { 631 *addr = (*(volatile u_int32_t *)(handle + offset)); 632 addr++; 633 } 634} 635 636 637 638/* 639 * Read `count' 1, 2, 4, or 8 byte quantities from bus space 640 * described by tag/handle and starting at `offset' and copy into 641 * buffer provided. 642 */ 643void 644rmi_pci_bus_space_read_region_1(void *t, bus_space_handle_t bsh, 645 bus_size_t offset, u_int8_t * addr, size_t count) 646{ 647 bus_addr_t baddr = bsh + offset; 648 649 while (count--) { 650 *addr++ = (*(volatile u_int8_t *)(baddr)); 651 baddr += 1; 652 } 653} 654 655void 656rmi_pci_bus_space_read_region_2(void *t, bus_space_handle_t bsh, 657 bus_size_t offset, u_int16_t * addr, size_t count) 658{ 659 bus_addr_t baddr = bsh + offset; 660 661 while (count--) { 662 *addr++ = (*(volatile u_int16_t *)(baddr)); 663 baddr += 2; 664 } 665} 666 667void 668rmi_pci_bus_space_read_region_4(void *t, bus_space_handle_t bsh, 669 bus_size_t offset, u_int32_t * addr, size_t count) 670{ 671 bus_addr_t baddr = bsh + offset; 672 673 while (count--) { 674 *addr++ = (*(volatile u_int32_t *)(baddr)); 675 baddr += 4; 676 } 677} 678 679 680void 681rmi_pci_bus_space_write_stream_1(void *t, bus_space_handle_t handle, 682 bus_size_t offset, u_int8_t value) 683{ 684 mips_sync(); 685 *(volatile u_int8_t *)(handle + offset) = value; 686} 687 688static void 689rmi_pci_bus_space_write_stream_2(void *t, bus_space_handle_t handle, 690 bus_size_t offset, u_int16_t value) 691{ 692 mips_sync(); 693 *(volatile u_int16_t *)(handle + offset) = value; 694} 695 696 697static void 698rmi_pci_bus_space_write_stream_4(void *t, bus_space_handle_t handle, 699 bus_size_t offset, u_int32_t value) 700{ 701 mips_sync(); 702 *(volatile u_int32_t *)(handle + offset) = value; 703} 704 705 706static void 707rmi_pci_bus_space_write_multi_stream_1(void *tag, bus_space_handle_t handle, 708 bus_size_t offset, const u_int8_t * addr, size_t count) 709{ 710 mips_sync(); 711 while (count--) { 712 (*(volatile u_int8_t *)(handle + offset)) = *addr; 713 addr++; 714 } 715} 716 717static void 718rmi_pci_bus_space_write_multi_stream_2(void *tag, bus_space_handle_t handle, 719 bus_size_t offset, const u_int16_t * addr, size_t count) 720{ 721 mips_sync(); 722 while (count--) { 723 (*(volatile u_int16_t *)(handle + offset)) = *addr; 724 addr++; 725 } 726} 727 728static void 729rmi_pci_bus_space_write_multi_stream_4(void *tag, bus_space_handle_t handle, 730 bus_size_t offset, const u_int32_t * addr, size_t count) 731{ 732 mips_sync(); 733 while (count--) { 734 (*(volatile u_int32_t *)(handle + offset)) = *addr; 735 addr++; 736 } 737} 738 739void 740rmi_pci_bus_space_write_region_2(void *t, 741 bus_space_handle_t bsh, 742 bus_size_t offset, 743 const u_int16_t * addr, 744 size_t count) 745{ 746 bus_addr_t baddr = (bus_addr_t) bsh + offset; 747 748 while (count--) { 749 (*(volatile u_int16_t *)(baddr)) = *addr; 750 addr++; 751 baddr += 2; 752 } 753} 754 755void 756rmi_pci_bus_space_write_region_4(void *t, bus_space_handle_t bsh, 757 bus_size_t offset, const u_int32_t * addr, size_t count) 758{ 759 bus_addr_t baddr = bsh + offset; 760 761 while (count--) { 762 (*(volatile u_int32_t *)(baddr)) = *addr; 763 addr++; 764 baddr += 4; 765 } 766} 767 768static void 769rmi_pci_bus_space_barrier(void *tag __unused, bus_space_handle_t bsh __unused, 770 bus_size_t offset __unused, bus_size_t len __unused, int flags) 771{ 772 773} 774