--- Log opened Fri Oct 30 09:45:18 2015 09:45 -!- wsa_ [~wsa@p4FE255A0.dip0.t-ipconnect.de] has joined #periperi 09:45 -!- Irssi: #periperi: Total of 7 nicks [0 ops, 0 halfops, 0 voices, 7 normal] 09:45 -!- Irssi: Join to #periperi was synced in 0 secs 09:45 < wsa_> hi there 09:45 < geertu> Hi Wolfram 09:46 < wsa_> hi geert 09:51 < wsa_> shall we maybe meet somewhere else (#periperi-io) so other people can stay here? 09:54 < geertu> Fine for me 09:55 < wsa_> done 09:55 < wsa_> I can't reproduce any of the issues pinchartl is seeing on Magnus' Koelsch :( 09:56 < wsa_> using his config and renesas-drivers-2015-10-27-v4.3-rc7 09:56 < geertu> Laurent and I both a version with a U-Boot that keeps MSTP clocks running 09:57 < geertu> I do see i2c failures with recent kernels since I enabled DU and friends 09:57 < wsa_> i should be able to emulate that with some 'mw' magic, or? 09:57 < geertu> I do run patches that disable MSTP clocks early in boot 09:57 < geertu> I'll email them to you 09:58 < wsa_> disable? 09:58 < wsa_> i thought your version keeps them running? 09:58 < geertu> yeah, I'm a bit puzzled, as I'd expect _you_ to see failures, not Laurent/ 09:59 -!- shimoda [~shimoda@relprex1.renesas.com] has joined #periperi 09:59 -!- morimoto [~user@relprex2.renesas.com] has joined #periperi 09:59 < geertu> Ah, the "real cold boot" of Koelsch now fails every day with an interrupt storm due to i2c not working for the regulator quirk 09:59 < wsa_> hmm, i think i'd prefer your uboot binary so can load it second stage to RAM 10:00 < wsa_> hello shimoda-san, morimoto-san 10:00 < shimoda> hello wolfram-san! 10:00 < wsa_> please join #periperi-io for the meeting 10:00 < shimoda> i got it 10:01 < wsa_> geertu: my best guess so far is that because of running clocks (and some weird leftover states from the bootloader) the IP core is an unknown state and not properly reinitialized yet 10:03 < geertu> Morimoto-san's "[PATCH] i2c: rcar: call rcar_i2c_init() after pm_runtime_get_sync()" doesn't fix the regylator interrupt storm at boot :-( 10:04 < geertu> If I keep he kernel running until the IRQ subsystem kills the interrupt, usually next reboot works 10:04 < wsa_> geertu: is that with koelsch, too? 10:04 < geertu> yep 10:04 < wsa_> please send me your .config 10:04 < wsa_> and now for the meeting 10:05 < geertu> It started earlier this week 10:05 < wsa_> :) 10:05 < geertu> Issue only happens if I power the board off for a long enough period (i.e. usually at night) 10:06 < geertu> OK, boot completed 10:06 < geertu> resetting 10:06 < geertu> and still working... 10:08 < wsa_> WTF? 10:48 < geertu> BTW, I will most probably have a day off on Wed and Thu --- Log closed Fri Oct 30 10:52:49 2015 --- Log opened Fri Oct 30 09:54:50 2015 09:54 -!- wsa_ [~wsa@p4FE255A0.dip0.t-ipconnect.de] has joined #periperi-io 09:54 -!- ServerMode/#periperi-io [+ns] by hitchcock.freenode.net 09:54 -!- Irssi: #periperi-io: Total of 1 nicks [1 ops, 0 halfops, 0 voices, 0 normal] 09:54 -!- Irssi: Join to #periperi-io was synced in 0 secs 09:55 -!- geertu [~geert@d54C36A7B.access.telenet.be] has joined #periperi-io 10:01 -!- morimoto [~user@relprex2.renesas.com] has joined #periperi-io 10:01 < morimoto> Hi 10:01 -!- shimoda [~shimoda@relprex1.renesas.com] has joined #periperi-io 10:02 <@wsa_> hello 10:02 < shimoda> hello! 10:02 <@wsa_> let's wait one more minute for simon 10:03 <@wsa_> if he doesn't show up, we'll put this topic last 10:05 <@wsa_> so 10:05 <@wsa_> let's get this done fast and concentrated :) 10:05 < shimoda> :) 10:05 -!- horms [~horms@penelope-musen.kanocho.kobe.vergenet.net] has joined #periperi-io 10:06 <@wsa_> SDHI DMA: is the BSP team happy with the suggested workflow? 10:06 <@wsa_> hi simon, just in time 10:06 < horms> Apologies for not being more punctual 10:06 < shimoda> i make some patches of SDHI DMA for BSP team :) 10:07 < shimoda> so this patch set will release in early next month as renesas-bsp 10:07 < shimoda> .git 10:07 <@wsa_> great! 10:07 <@wsa_> then we can have a look about further refactoring 10:08 <@wsa_> how did PTP come along? 10:08 < shimoda> yes, I think we have to refactor the tmio driver somehow :) 10:09 < horms> wsa_: regarding ptp 10:09 < horms> I have looked over the bsp after learning it contains the required functionality 10:09 <@wsa_> shimoda: (yes, i think so, too. but let's talk over the code when it is available in git) 10:10 < horms> it seems to me that the only relevant code that is not upstream is to correct an assubmption that the input clock is running at 130MHz: as was the case on Gen2. 10:11 < horms> On the r8a7795 I believe the situation is that it should run at 133MHz, but due to the extal being 16.6Mhz rather than 33MHz it runs at half that speed. i.e. 66.6Mhz. 10:11 < shimoda> wsa_: agreed 10:11 < horms> I think the answer is to have the driver use the speed of its clock to set the register in question 10:11 < horms> i plan to see about implementing that 10:12 <@wsa_> okay, this sounds like a bit of work, but no major obstacle? 10:12 < horms> yes, i think so 10:12 <@wsa_> knock on wood :) 10:12 < horms> I think the main questin will be if the binding needs changing. to my mind the answer is "hopefully not" 10:13 < horms> in theory other clocks could be relevant 10:13 < horms> but in practice the hw only accepts using the high-speed peripheral clock (or what that was renamed to in gen3) 10:13 < horms> so fingers crossed it should not be too difficult to resolve in an upstreamable manner 10:14 * wsa_ crosses fingers 10:14 <@wsa_> thanks simon 10:14 <@wsa_> about PCIE 10:14 <@wsa_> I tested Phil's patches, and they turned out to go into the right directon, but being unusable for now 10:15 <@wsa_> even introducing a build error 10:15 < horms> :( 10:15 <@wsa_> Bjorn wants to drop the branch which would also be my favourite solution 10:16 < horms> thats fine by me. I'll be more careful with Phil's patches in future 10:16 <@wsa_> but i didn't want to suggest it directly because i wanted to be careful (from a diplomatic point of view) 10:16 < horms> do you need me to respond to the thread? 10:17 <@wsa_> if you want, but i don't think it is needed 10:17 < horms> ok 10:17 < horms> i prefer to take a back-seat with PCIe unless action is required 10:17 < horms> if the wheels fall off I'm happy to step in :) 10:18 <@wsa_> okay, thanks 10:18 <@wsa_> watchdog 10:18 < horms> from my pov phil should know what he is doing. its unfortunate if that isn't the case 10:18 < horms> but i will be more watchful 10:19 <@wsa_> make sure his patches get build tested 10:19 < horms> can do 10:19 <@wsa_> i also think that he knows what he is doing in general 10:19 <@wsa_> but this series wasn't his best one 10:19 <@wsa_> for whatever reason 10:19 < horms> ok 10:19 <@wsa_> watchdog ;) 10:20 <@wsa_> my current plan is to release the next version in the second week of November 10:20 <@wsa_> shimoda: will this be enough? or is it needed before that? 10:21 < shimoda> BSP point of view, they need next Wed. :) 10:22 < shimoda> however, they can use your current patch too 10:22 <@wsa_> i'll se what i can do 10:23 <@wsa_> how are the other deadlines? 10:23 < shimoda> thank you! 10:23 <@wsa_> did something urgent show up I missed so far? 10:24 < shimoda> about i/o group, no other deadlines, i think 10:25 <@wsa_> good 10:26 <@wsa_> so major topic seems to be i2c now :( 10:26 <@wsa_> i'll do some more debugging but if I can't reproduce the issues, I probably have to revert the latest r-car series 10:26 < shimoda> i think so ;) 10:27 < morimoto> About Laurent issue do you mean ? 10:27 <@wsa_> geert has some, too 10:27 <@wsa_> all on koelsch 10:28 < morimoto> OK 10:28 <@wsa_> but it doesn't show on Magnus Koelsch 10:28 <@wsa_> where i have remote access to 10:28 < morimoto> which ES number used ? 10:29 < morimoto> do you know ? 10:29 <@wsa_> I asked geert for his u-boot binary so I can start this second stage from RAM 10:29 < geertu> how do I get the U-Boot binary? 10:29 < geertu> raw QSPI contents 10:29 < geertu> ? 10:29 < geertu> Detected Renesas R-Car M2-W ES1.0 10:29 <@wsa_> I thought you have patches to u-boot? 10:29 <@wsa_> Laurent reported: 10:29 <@wsa_> RTPORC7791SEB00010S 10:29 <@wsa_> KOELSCH SN.057 10:30 < geertu> My Koelsch is SN.192, and Laurent's is older,so it should be ES1.0 too 10:30 < geertu> Mine ends with 11S 10:31 < geertu> Aha, different board revision too 10:31 <@wsa_> geertu: are those identification patches some way upstream? 10:31 < geertu> No, old version on periperi 10:31 < shimoda> about u-boot code of gen2: git://git.denx.de/u-boot-sh.git 10:31 < geertu> Can email out a new one, moved to drivers/soc and supporting H3 and M3-W 10:32 < shimoda> origin/renesas/bsp/rcar-gen2-7 branch is the latest 10:32 < geertu> yep, checkout b6af5fc 10:32 < geertu> Probably Wolfram (Magnus) has the latest version, in Dublin we saw it was from spring 2015 10:32 <@wsa_> geertu: new version sounds good 10:32 < geertu> So I have ver=U-Boot 2013.01.01-gb6af5fc (Nov 12 2013 - 14:33:51) 10:33 < geertu> wsa_: but you don't see the issue on new versions? 10:33 <@wsa_> nope 10:33 <@wsa_> morimoto: can we get a "changelog" for Koelsch board revisions? 10:33 <@wsa_> does something like this exist? 10:34 < morimoto> do you mean "board" changelog ? 10:34 <@wsa_> yes 10:34 < morimoto> let me check 10:35 <@wsa_> okay, i more and more get the impression i should revert the changes 10:35 < geertu> "[PATCH] [LOCAL] ARM: shmobile: Print SoC Product Version" sent to periperi 10:35 <@wsa_> they need more time (and not quick hacks) on some Koelsch boards 10:37 <@wsa_> please note that i am away next week 10:38 <@wsa_> i might be able to respond to emails 10:38 <@wsa_> i think we are done unless one of you has a topic left 10:39 < horms> none from my side 10:40 < morimoto> Can I interrupt ? The issue if for HDMI chip access ? 10:40 < morimoto> /if/is/ 10:40 <@wsa_> yes 10:41 <@wsa_> adv7180 10:41 < morimoto> Thanks 10:41 < geertu> "Your message to periperi awaits moderator approval" 10:41 < horms> oh! 10:41 < geertu> geert+renesas is not a member ;-) 10:42 < horms> did you post just now? 10:42 < geertu> yes 10:42 < geertu> the patch I mentioned above 10:43 < horms> ok, i'll try and approve the post and whitelist your address 10:43 < geertu> thx! 10:44 <@wsa_> morimoto: can you email me your findings? 10:44 <@wsa_> they are much appreciated 10:45 <@wsa_> then, we could close this meeting 10:45 <@wsa_> and go for weekend :D (sooner or later, depending on TZ) 10:46 < shimoda> yes, thank you for the meeting! 10:46 < morimoto> Now, HW team is not here, I asked to them via email. please wait 10:46 <@wsa_> will do 10:46 <@wsa_> cya all! 10:47 < geertu> bye! 10:47 < morimoto> I will have day off on Mon, and Tue is Holiday in Japan. 10:47 < geertu> Have a nice weekend 10:47 < horms> enjoy 10:47 < morimoto> shimoda-san is OK in Mon, so please care it >> shimoda-san 10:47 < shimoda> have a nice weekend! 10:47 < morimoto> bye --- Log closed Fri Oct 30 10:52:49 2015 247' href='#n247'>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
/**
 * \file drm_bufs.c
 * Generic buffer template
 *
 * \author Rickard E. (Rik) Faith <faith@valinux.com>
 * \author Gareth Hughes <gareth@valinux.com>
 */

/*
 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
 *
 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include <linux/vmalloc.h>
#include "drmP.h"

 /**
 * Adjusts the memory offset to its absolute value according to the mapping
 * type.  Adds the map to the map list drm_device::maplist. Adds MTRR's where
 * applicable and if supported by the kernel.
 */
int drm_initmap(drm_device_t * dev, unsigned int offset, unsigned int size,
		int type, int flags)
{
	drm_map_t *map;
	drm_map_list_t *list;

	DRM_DEBUG("\n");

	if ((offset & (~PAGE_MASK)) || (size & (~PAGE_MASK)))
		return -EINVAL;
#if !defined(__sparc__) && !defined(__alpha__)
	if (offset + size < offset || offset < virt_to_phys(high_memory))
		return -EINVAL;
#endif
	if (!(list = drm_alloc(sizeof(*list), DRM_MEM_MAPS)))
		return -ENOMEM;
	memset(list, 0, sizeof(*list));

	if (!(map = drm_alloc(sizeof(*map), DRM_MEM_MAPS))) {
		drm_free(list, sizeof(*list), DRM_MEM_MAPS);
		return -ENOMEM;
	}

	*map = (drm_map_t) {
	.offset = offset,.size = size,.type = type,.flags =
		    flags,.mtrr = -1,.handle = 0,};
	list->map = map;

	DRM_DEBUG("initmap offset = 0x%08lx, size = 0x%08lx, type = %d\n",
		  map->offset, map->size, map->type);

#ifdef __alpha__
	map->offset += dev->hose->mem_space->start;
#endif
	if (drm_core_has_MTRR(dev)) {
		if (map->type == _DRM_FRAME_BUFFER ||
		    (map->flags & _DRM_WRITE_COMBINING)) {
			map->mtrr = mtrr_add(map->offset, map->size,
					     MTRR_TYPE_WRCOMB, 1);
		}
	}

	if (map->type == _DRM_REGISTERS)
		map->handle = drm_ioremap(map->offset, map->size, dev);

	down(&dev->struct_sem);
	list_add(&list->head, &dev->maplist->head);
	up(&dev->struct_sem);

	dev->driver->permanent_maps = 1;
	DRM_DEBUG("finished\n");

	return 0;
}
EXPORT_SYMBOL(drm_initmap);

/**
 * Ioctl to specify a range of memory that is available for mapping by a non-root process.
 *
 * \param inode device inode.
 * \param filp file pointer.
 * \param cmd command.
 * \param arg pointer to a drm_map structure.
 * \return zero on success or a negative value on error.
 *
 * Adjusts the memory offset to its absolute value according to the mapping
 * type.  Adds the map to the map list drm_device::maplist. Adds MTRR's where
 * applicable and if supported by the kernel.
 */
int drm_addmap(struct inode *inode, struct file *filp,
	       unsigned int cmd, unsigned long arg)
{
	drm_file_t *priv = filp->private_data;
	drm_device_t *dev = priv->head->dev;
	drm_map_t *map;
	drm_map_t __user *argp = (void __user *)arg;
	drm_map_list_t *list;

	if (!(filp->f_mode & 3))
		return -EACCES;	/* Require read/write */

	map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
	if (!map)
		return -ENOMEM;

	if (copy_from_user(map, argp, sizeof(*map))) {
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		return -EFAULT;
	}

	/* Only allow shared memory to be removable since we only keep enough
	 * book keeping information about shared memory to allow for removal
	 * when processes fork.
	 */
	if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		return -EINVAL;
	}
	DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
		  map->offset, map->size, map->type);
	if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		return -EINVAL;
	}
	map->mtrr = -1;
	map->handle = NULL;

	switch (map->type) {
	case _DRM_REGISTERS:
	case _DRM_FRAME_BUFFER:{
			/* after all the drivers switch to permanent mapping this should just return an error */
			struct list_head *_list;

			/* If permanent maps are implemented, maps must match */
			if (dev->driver->permanent_maps) {
				DRM_DEBUG
				    ("Looking for: offset = 0x%08lx, size = 0x%08lx, type = %d\n",
				     map->offset, map->size, map->type);
				list_for_each(_list, &dev->maplist->head) {
					drm_map_list_t *_entry =
					    list_entry(_list, drm_map_list_t,
						       head);
					DRM_DEBUG
					    ("Checking: offset = 0x%08lx, size = 0x%08lx, type = %d\n",
					     _entry->map->offset,
					     _entry->map->size,
					     _entry->map->type);
					if (_entry->map
					    && map->type == _entry->map->type
					    && map->offset ==
					    _entry->map->offset) {
						_entry->map->size = map->size;
						drm_free(map, sizeof(*map),
							 DRM_MEM_MAPS);
						map = _entry->map;
						DRM_DEBUG
						    ("Found existing: offset = 0x%08lx, size = 0x%08lx, type = %d\n",
						     map->offset, map->size,
						     map->type);
						goto found_it;
					}
				}
				/* addmap didn't match an existing permanent map, that's an error */
				return -EINVAL;
			}
#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__)
			if (map->offset + map->size < map->offset ||
			    map->offset < virt_to_phys(high_memory)) {
				drm_free(map, sizeof(*map), DRM_MEM_MAPS);
				return -EINVAL;
			}
#endif
#ifdef __alpha__
			map->offset += dev->hose->mem_space->start;
#endif
			if (drm_core_has_MTRR(dev)) {
				if (map->type == _DRM_FRAME_BUFFER ||
				    (map->flags & _DRM_WRITE_COMBINING)) {
					map->mtrr =
					    mtrr_add(map->offset, map->size,
						     MTRR_TYPE_WRCOMB, 1);
				}
			}
			if (map->type == _DRM_REGISTERS)
				map->handle =
				    drm_ioremap(map->offset, map->size, dev);
			break;
		}
	case _DRM_SHM:
		map->handle = vmalloc_32(map->size);
		DRM_DEBUG("%lu %d %p\n",
			  map->size, drm_order(map->size), map->handle);
		if (!map->handle) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -ENOMEM;
		}
		map->offset = (unsigned long)map->handle;
		if (map->flags & _DRM_CONTAINS_LOCK) {
			/* Prevent a 2nd X Server from creating a 2nd lock */
			if (dev->lock.hw_lock != NULL) {
				vfree(map->handle);
				drm_free(map, sizeof(*map), DRM_MEM_MAPS);
				return -EBUSY;
			}
			dev->sigdata.lock = dev->lock.hw_lock = map->handle;	/* Pointer to lock */
		}
		break;
	case _DRM_AGP:
		if (drm_core_has_AGP(dev)) {
#ifdef __alpha__
			map->offset += dev->hose->mem_space->start;
#endif
			map->offset += dev->agp->base;
			map->mtrr = dev->agp->agp_mtrr;	/* for getmap */
		}
		break;
	case _DRM_SCATTER_GATHER:
		if (!dev->sg) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -EINVAL;
		}
		map->offset += dev->sg->handle;
		break;
	case _DRM_CONSISTENT: {
		/* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G.
		 * As we're limiting the address to 2^32-1 (or less),
		 * casting it down to 32 bits is no problem, but we
		 * need to point to a 64bit variable first. */
		dma_addr_t bus_addr;
		map->handle = drm_pci_alloc(dev, map->size, map->size,
					    0xffffffffUL, &bus_addr);
		map->offset = (unsigned long)bus_addr;
		if (!map->handle) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -ENOMEM;
		}
		break;
	}
	default:
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		return -EINVAL;
	}

	list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
	if (!list) {
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		return -EINVAL;
	}
	memset(list, 0, sizeof(*list));
	list->map = map;

	down(&dev->struct_sem);
	list_add(&list->head, &dev->maplist->head);
	up(&dev->struct_sem);
      found_it:
	if (copy_to_user(argp, map, sizeof(*map)))
		return -EFAULT;
	if (map->type != _DRM_SHM) {
		if (copy_to_user(&argp->handle,
				 &map->offset, sizeof(map->offset)))
			return -EFAULT;
	}
	return 0;
}

/**
 * Remove a map private from list and deallocate resources if the mapping
 * isn't in use.
 *
 * \param inode device inode.
 * \param filp file pointer.
 * \param cmd command.
 * \param arg pointer to a drm_map_t structure.
 * \return zero on success or a negative value on error.
 *
 * Searches the map on drm_device::maplist, removes it from the list, see if
 * its being used, and free any associate resource (such as MTRR's) if it's not
 * being on use.
 *
 * \sa addmap().
 */
int drm_rmmap(struct inode *inode, struct file *filp,
	      unsigned int cmd, unsigned long arg)
{
	drm_file_t *priv = filp->private_data;
	drm_device_t *dev = priv->head->dev;
	struct list_head *list;
	drm_map_list_t *r_list = NULL;
	drm_vma_entry_t *pt, *prev;
	drm_map_t *map;
	drm_map_t request;
	int found_maps = 0;

	if (copy_from_user(&request, (drm_map_t __user *) arg, sizeof(request))) {
		return -EFAULT;
	}

	down(&dev->struct_sem);
	list = &dev->maplist->head;
	list_for_each(list, &dev->maplist->head) {
		r_list = list_entry(list, drm_map_list_t, head);

		if (r_list->map &&
		    r_list->map->handle == request.handle &&
		    r_list->map->flags & _DRM_REMOVABLE)
			break;
	}

	/* List has wrapped around to the head pointer, or its empty we didn't
	 * find anything.
	 */
	if (list == (&dev->maplist->head)) {
		up(&dev->struct_sem);
		return -EINVAL;
	}
	map = r_list->map;

	/* Register and framebuffer maps are permanent */
	if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
		up(&dev->struct_sem);
		return 0;
	}
	list_del(list);
	drm_free(list, sizeof(*list), DRM_MEM_MAPS);

	for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) {
		if (pt->vma->vm_private_data == map)
			found_maps++;
	}

	if (!found_maps) {
		switch (map->type) {
		case _DRM_REGISTERS:
		case _DRM_FRAME_BUFFER:
			break;	/* Can't get here, make compiler happy */
		case _DRM_SHM:
			vfree(map->handle);
			break;
		case _DRM_AGP:
		case _DRM_SCATTER_GATHER:
			break;
		case _DRM_CONSISTENT:
			drm_pci_free(dev, map->size, map->handle, map->offset);
			break;
		}
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
	}
	up(&dev->struct_sem);
	return 0;
}

/**
 * Cleanup after an error on one of the addbufs() functions.
 *
 * \param dev DRM device.
 * \param entry buffer entry where the error occurred.
 *
 * Frees any pages and buffers associated with the given entry.
 */
static void drm_cleanup_buf_error(drm_device_t * dev, drm_buf_entry_t * entry)
{
	int i;

	if (entry->seg_count) {
		for (i = 0; i < entry->seg_count; i++) {
			if (entry->seglist[i]) {
				drm_free_pages(entry->seglist[i],
					       entry->page_order, DRM_MEM_DMA);
			}
		}
		drm_free(entry->seglist,
			 entry->seg_count *
			 sizeof(*entry->seglist), DRM_MEM_SEGS);

		entry->seg_count = 0;
	}

	if (entry->buf_count) {
		for (i = 0; i < entry->buf_count; i++) {
			if (entry->buflist[i].dev_private) {
				drm_free(entry->buflist[i].dev_private,
					 entry->buflist[i].dev_priv_size,
					 DRM_MEM_BUFS);
			}
		}
		drm_free(entry->buflist,
			 entry->buf_count *
			 sizeof(*entry->buflist), DRM_MEM_BUFS);

		entry->buf_count = 0;
	}
}

#if __OS_HAS_AGP
/**
 * Add AGP buffers for DMA transfers (ioctl).
 *
 * \param inode device inode.
 * \param filp file pointer.
 * \param cmd command.
 * \param arg pointer to a drm_buf_desc_t request.
 * \return zero on success or a negative number on failure.
 *
 * After some sanity checks creates a drm_buf structure for each buffer and
 * reallocates the buffer list of the same size order to accommodate the new
 * buffers.
 */
int drm_addbufs_agp(struct inode *inode, struct file *filp,
		    unsigned int cmd, unsigned long arg)
{
	drm_file_t *priv = filp->private_data;
	drm_device_t *dev = priv->head->dev;
	drm_device_dma_t *dma = dev->dma;
	drm_buf_desc_t request;
	drm_buf_entry_t *entry;
	drm_buf_t *buf;
	unsigned long offset;
	unsigned long agp_offset;
	int count;
	int order;
	int size;
	int alignment;
	int page_order;
	int total;
	int byte_count;
	int i;
	drm_buf_t **temp_buflist;
	drm_buf_desc_t __user *argp = (void __user *)arg;

	if (!dma)
		return -EINVAL;

	if (copy_from_user(&request, argp, sizeof(request)))
		return -EFAULT;

	count = request.count;
	order = drm_order(request.size);
	size = 1 << order;

	alignment = (request.flags & _DRM_PAGE_ALIGN)
	    ? PAGE_ALIGN(size) : size;
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

	byte_count = 0;
	agp_offset = dev->agp->base + request.agp_start;

	DRM_DEBUG("count:      %d\n", count);
	DRM_DEBUG("order:      %d\n", order);
	DRM_DEBUG("size:       %d\n", size);
	DRM_DEBUG("agp_offset: %lu\n", agp_offset);
	DRM_DEBUG("alignment:  %d\n", alignment);
	DRM_DEBUG("page_order: %d\n", page_order);
	DRM_DEBUG("total:      %d\n", total);

	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
	if (dev->queue_count)
		return -EBUSY;	/* Not while in use */

	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
		return -EBUSY;
	}
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);

	down(&dev->struct_sem);
	entry = &dma->bufs[order];
	if (entry->buf_count) {
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;	/* May only call once for each order */
	}

	if (count < 0 || count > 4096) {
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
		return -EINVAL;
	}

	entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
				   DRM_MEM_BUFS);
	if (!entry->buflist) {
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;
	}
	memset(entry->buflist, 0, count * sizeof(*entry->buflist));

	entry->buf_size = size;
	entry->page_order = page_order;

	offset = 0;

	while (entry->buf_count < count) {
		buf = &entry->buflist[entry->buf_count];
		buf->idx = dma->buf_count + entry->buf_count;
		buf->total = alignment;
		buf->order = order;
		buf->used = 0;

		buf->offset = (dma->byte_count + offset);
		buf->bus_address = agp_offset + offset;
		buf->address = (void *)(agp_offset + offset);
		buf->next = NULL;
		buf->waiting = 0;
		buf->pending = 0;
		init_waitqueue_head(&buf->dma_wait);
		buf->filp = NULL;

		buf->dev_priv_size = dev->driver->dev_priv_size;
		buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
		if (!buf->dev_private) {
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
			drm_cleanup_buf_error(dev, entry);
			up(&dev->struct_sem);
			atomic_dec(&dev->buf_alloc);
			return -ENOMEM;
		}
		memset(buf->dev_private, 0, buf->dev_priv_size);

		DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);

		offset += alignment;
		entry->buf_count++;
		byte_count += PAGE_SIZE << page_order;
	}

	DRM_DEBUG("byte_count: %d\n", byte_count);

	temp_buflist = drm_realloc(dma->buflist,
				   dma->buf_count * sizeof(*dma->buflist),
				   (dma->buf_count + entry->buf_count)
				   * sizeof(*dma->buflist), DRM_MEM_BUFS);
	if (!temp_buflist) {
		/* Free the entry because it isn't valid */
		drm_cleanup_buf_error(dev, entry);
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

	for (i = 0; i < entry->buf_count; i++) {