#!/bin/sh
#
# Copyright (c) 2020, The ULBSD Project
# Copyright (c) 2020, The NomadBSD Project
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $Id: ulbsd-gfx-ati-amd 34 2020-07-27 21:12:11Z sv $

path_ati_cfg="/usr/local/etc/X11/xorg.conf.d/20-ati.conf"
path_amd_cfg="/usr/local/etc/X11/xorg.conf.d/20-amdgpu.conf"

data_radeon() {
cat << radeon_END
0x4144
0x4145
0x4146
0x4147
0x4E44
0x4E45
0x4E46
0x4E47
0x4148
0x4149
0x414A
0x414B
0x4E48
0x4E49
0x4E4A
0x4E4B
0x4150
0x4151
0x4152
0x4153
0x4154
0x4155
0x4156
0x4E50
0x4E51
0x4E52
0x4E53
0x4E54
0x4E56
0x3150
0x3151
0x3152
0x3154
0x3155
0x3E50
0x3E54
0x5460
0x5462
0x5464
0x5b60
0x5b62
0x5b63
0x5b64
0x5b65
0x5a41
0x5a42
0x5a61
0x5a62
0x5954
0x5955
0x5974
0x5975
0x4A48
0x4A49
0x4A4A
0x4A4B
0x4A4C
0x4A4D
0x4A4E
0x4A4F
0x4A50
0x4A54
0x4B48
0x4B49
0x4B4A
0x4B4B
0x4B4C
0x5548
0x5549
0x554A
0x554B
0x554C
0x554D
0x554E
0x554F
0x5550
0x5551
0x5552
0x5554
0x5d48
0x5d49
0x5d4a
0x5d4c
0x5d4d
0x5d4e
0x5d4f
0x5d50
0x5d52
0x5d57
0x564A
0x564B
0x564F
0x5652
0x5653
0x5657
0x5e48
0x5e4a
0x5e4b
0x5e4c
0x5e4d
0x5e4f
0x793f
0x7941
0x7942
0x791e
0x791f
0x796c
0x796d
0x796e
0x796f
0x7140
0x7141
0x7142
0x7143
0x7144
0x7145
0x7146
0x7147
0x7149
0x714A
0x714B
0x714C
0x714D
0x714E
0x714F
0x7151
0x7152
0x7153
0x715E
0x715F
0x7180
0x7181
0x7183
0x7186
0x7187
0x7188
0x718A
0x718B
0x718C
0x718D
0x718F
0x7193
0x7196
0x719B
0x719F
0x7200
0x7210
0x7211
0x7100
0x7101
0x7102
0x7103
0x7104
0x7105
0x7106
0x7108
0x7109
0x710A
0x710B
0x710C
0x710E
0x710F
0x71C0
0x71C1
0x71C2
0x71C3
0x71C4
0x71C5
0x71C6
0x71C7
0x71CD
0x71CE
0x71D2
0x71D4
0x71D5
0x71D6
0x71DA
0x71DE
0x7281
0x7283
0x7287
0x7290
0x7291
0x7293
0x7297
0x7280
0x7288
0x7289
0x728B
0x728C
0x7240
0x7243
0x7244
0x7245
0x7246
0x7247
0x7248
0x7249
0x724A
0x724B
0x724C
0x724D
0x724E
0x724F
0x7284
0x9400
0x9401
0x9402
0x9403
0x9405
0x940A
0x940B
0x940F
0x94C0
0x94C1
0x94C3
0x94C4
0x94C5
0x94C6
0x94C7
0x94C8
0x94C9
0x94CB
0x94CC
0x94CD
0x9580
0x9581
0x9583
0x9586
0x9587
0x9588
0x9589
0x958A
0x958B
0x958C
0x958D
0x958E
0x958F
0x95C0
0x95C2
0x95C4
0x95C5
0x95C6
0x95C7
0x95C9
0x95CC
0x95CD
0x95CE
0x95CF
0x9590
0x9591
0x9593
0x9595
0x9596
0x9597
0x9598
0x9599
0x959B
0x9500
0x9501
0x9504
0x9505
0x9506
0x9507
0x9508
0x9509
0x950F
0x9511
0x9515
0x9517
0x9519
0x9610
0x9611
0x9612
0x9613
0x9614
0x9615
0x9616
0x9710
0x9711
0x9712
0x9713
0x9714
0x9715
0x9440
0x9441
0x9442
0x9443
0x9444
0x9446
0x944A
0x944B
0x944C
0x944E
0x9450
0x9452
0x9456
0x945A
0x945B
0x945E
0x9460
0x9462
0x946A
0x946B
0x947A
0x947B
0x9480
0x9487
0x9488
0x9489
0x948A
0x948F
0x9490
0x9491
0x9495
0x9498
0x949C
0x949E
0x949F
0x9540
0x9541
0x9542
0x954E
0x954F
0x9552
0x9553
0x9555
0x9557
0x955f
0x94A0
0x94A1
0x94A3
0x94B1
0x94B3
0x94B4
0x94B5
0x94B9
0x68e0
0x68e1
0x68e4
0x68e5
0x68e8
0x68e9
0x68f1
0x68f2
0x68f8
0x68f9
0x68fa
0x68fe
0x68c0
0x68c1
0x68c7
0x68c8
0x68c9
0x68d8
0x68d9
0x68da
0x68de
0x68a0
0x68a1
0x68a8
0x68a9
0x68b0
0x68b8
0x68b9
0x68ba
0x68be
0x68bf
0x6880
0x6888
0x6889
0x688A
0x688C
0x688D
0x6898
0x6899
0x689b
0x689e
0x9802
0x9803
0x9804
0x9805
0x9806
0x9807
0x9808
0x9809
0x980A
0x9640
0x9641
0x9642
0x9643
0x9644
0x9645
0x9647
0x9648
0x9649
0x964a
0x964b
0x964c
0x964e
0x964f
0x9642
0x9643
0x9644
0x9645
0x9649
0x9900
0x9901
0x9903
0x9904
0x9905
0x9906
0x9907
0x9908
0x9909
0x990A
0x990B
0x990C
0x990D
0x990E
0x990F
0x9910
0x9913
0x9917
0x9918
0x9919
0x9990
0x9991
0x9992
0x9993
0x9994
0x9995
0x9996
0x9997
0x9998
0x9999
0x999A
0x999B
0x999C
0x999D
0x99A0
0x99A2
0x99A4
0x6720
0x6721
0x6722
0x6723
0x6724
0x6725
0x6726
0x6727
0x6728
0x6729
0x6738
0x6739
0x673e
0x6740
0x6741
0x6742
0x6743
0x6744
0x6745
0x6746
0x6747
0x6748
0x6749
0x674A
0x6750
0x6751
0x6758
0x6759
0x675B
0x675D
0x675F
0x6840
0x6841
0x6842
0x6843
0x6849
0x6850
0x6858
0x6859
0x6760
0x6761
0x6762
0x6763
0x6764
0x6765
0x6766
0x6767
0x6768
0x6770
0x6771
0x6772
0x6778
0x6779
0x677B
radeon_END
}

amd_xorg_cfg() {
cat <<amd_xorg_cfg_END
Section "Device"
    Identifier "AMD"
    Driver     "amdgpu"
    Option     "DRI" "3"
    Option     "TearFree" "true"
    BusID      "PCI:$busID"
EndSection
amd_xorg_cfg_END
}

ati_xorg_cfg() {
cat <<ati_xorg_cfg_END
Section "Device"
    Identifier "ATI"
    Driver     "radeon"
    BusID      "PCI:$busID"
EndSection
ati_xorg_cfg_END
}

unit=$(sysctl -n hw.pci.default_vgapci_unit)
[ -z "$unit" -o "$unit" =  "-1" ] && unit=0
config=$(pciconf -lv | grep ^vgapci${unit} | head -1 | \
	sed -E 's#.* chip=0x([0-9a-z]{4})([0-9a-z]{4}).*$#\1:\2#')
busID=$(pciconf -lv | grep ^vgapci${unit} | head -1 | \
	sed -E 's/^vgapci[0-9]?@pci[0-9]:([0-9:]+):.*$/\1/')

vendor=$(echo "$config" | cut -d: -f2)
device=$(echo "$config" | cut -d: -f1)
[ "$vendor" = 1002 ] || exit 1

if data_radeon | grep -iq ${device}; then
	ati_xorg_cfg > "${path_ati_cfg}"
	sysrc kld_list+=/boot/modules/radeonkms.ko
	kldload -n /boot/modules/radeonkms.ko
	echo "modules/radeonkms.ko" > /var/run/gfxdriver
else
	amd_xorg_cfg > "${path_amd_cfg}"
	sysrc kld_list+=/boot/modules/amdgpu.ko
	kldload -n /boot/modules/amdgpu.ko
	echo "modules/amdgpu.ko" > /var/run/gfxdriver
fi
exit 0
