Merge tag 'drm-for-v4.9' of git://people.freedesktop.org/~airlied/linux
authorLinus Torvalds <torvalds@linux-foundation.org>
Wed, 12 Oct 2016 01:12:22 +0000 (18:12 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 12 Oct 2016 01:12:22 +0000 (18:12 -0700)
Pull drm updates from Dave Airlie:
 "Core:
   - Fence destaging work
   - DRIVER_LEGACY to split off legacy drm drivers
   - drm_mm refactoring
   - Splitting drm_crtc.c into chunks and documenting better
   - Display info fixes
   - rbtree support for prime buffer lookup
   - Simple VGA DAC driver

  Panel:
   - Add Nexus 7 panel
   - More simple panels

  i915:
   - Refactoring GEM naming
   - Refactored vma/active tracking
   - Lockless request lookups
   - Better stolen memory support
   - FBC fixes
   - SKL watermark fixes
   - VGPU improvements
   - dma-buf fencing support
   - Better DP dongle support

  amdgpu:
   - Powerplay for Iceland asics
   - Improved GPU reset support
   - UVD/VEC powergating support for CZ/ST
   - Preinitialised VRAM buffer support
   - Virtual display support
   - Initial SI support
   - GTT rework
   - PCI shutdown callback support
   - HPD IRQ storm fixes

  amdkfd:
   - bugfixes

  tilcdc:
   - Atomic modesetting support

  mediatek:
   - AAL + GAMMA engine support
   - Hook up gamma LUT
   - Temporal dithering support

  imx:
   - Pixel clock from devicetree
   - drm bridge support for LVDS bridges
   - active plane reconfiguration
   - VDIC deinterlacer support
   - Frame synchronisation unit support
   - Color space conversion support

  analogix:
   - PSR support
   - Better panel on/off support

  rockchip:
   - rk3399 vop/crtc support
   - PSR support

  vc4:
   - Interlaced vblank timing
   - 3D rendering CPU overhead reduction
   - HDMI output fixes

  tda998x:
   - HDMI audio ASoC support

  sunxi:
   - Allwinner A33 support
   - better TCON support

  msm:
   - DT binding cleanups
   - Explicit fence-fd support

  sti:
   - remove sti415/416 support

  etnaviv:
   - MMUv2 refactoring
   - GC3000 support

  exynos:
   - Refactoring HDMI DCC/PHY
   - G2D pm regression fix
   - Page fault issues with wait for vblank

  There is no nouveau work in this tree, as Ben didn't get a pull
  request in, and he was fighting moving to atomic and adding mst
  support, so maybe best it waits for a cycle"

* tag 'drm-for-v4.9' of git://people.freedesktop.org/~airlied/linux: (1412 commits)
  drm/crtc: constify drm_crtc_index parameter
  drm/i915: Fix conflict resolution from backmerge of v4.8-rc8 to drm-next
  drm/i915/guc: Unwind GuC workqueue reservation if request construction fails
  drm/i915: Reset the breadcrumbs IRQ more carefully
  drm/i915: Force relocations via cpu if we run out of idle aperture
  drm/i915: Distinguish last emitted request from last submitted request
  drm/i915: Allow DP to work w/o EDID
  drm/i915: Move long hpd handling into the hotplug work
  drm/i915/execlists: Reinitialise context image after GPU hang
  drm/i915: Use correct index for backtracking HUNG semaphores
  drm/i915: Unalias obj->phys_handle and obj->userptr
  drm/i915: Just clear the mmiodebug before a register access
  drm/i915/gen9: only add the planes actually affected by ddb changes
  drm/i915: Allow PCH DPLL sharing regardless of DPLL_SDVO_HIGH_SPEED
  drm/i915/bxt: Fix HDMI DPLL configuration
  drm/i915/gen9: fix the watermark res_blocks value
  drm/i915/gen9: fix plane_blocks_per_line on watermarks calculations
  drm/i915/gen9: minimum scanlines for Y tile is not always 4
  drm/i915/gen9: fix the WaWmMemoryReadLatency implementation
  drm/i915/kbl: KBL also needs to run the SAGV code
  ...

13 files changed:
1  2 
Documentation/gpu/index.rst
MAINTAINERS
arch/arm/boot/dts/am335x-boneblack.dts
arch/x86/kernel/early-quirks.c
drivers/dma-buf/sync_debug.c
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
drivers/gpu/drm/armada/armada_gem.c
drivers/gpu/drm/drm_dp_helper.c
drivers/gpu/drm/i915/i915_gem.c
drivers/gpu/drm/i915/i915_gem_execbuffer.c
drivers/gpu/drm/nouveau/nouveau_bo.c
drivers/gpu/drm/radeon/si_dpm.c
include/uapi/linux/Kbuild

@@@ -12,10 -12,4 +12,11 @@@ Linux GPU Driver Developer's Guid
     drm-uapi
     i915
     vga-switcheroo
+    vgaarbiter
 +
 +.. only::  subproject
 +
 +   Indices
 +   =======
 +
 +   * :ref:`genindex`
diff --combined MAINTAINERS
@@@ -636,15 -636,6 +636,15 @@@ F:       drivers/tty/serial/altera_jtaguart.
  F:    include/linux/altera_uart.h
  F:    include/linux/altera_jtaguart.h
  
 +AMAZON ETHERNET DRIVERS
 +M:    Netanel Belgazal <netanel@annapurnalabs.com>
 +R:    Saeed Bishara <saeed@annapurnalabs.com>
 +R:    Zorik Machulsky <zorik@annapurnalabs.com>
 +L:    netdev@vger.kernel.org
 +S:    Supported
 +F:    Documentation/networking/ena.txt
 +F:    drivers/net/ethernet/amazon/
 +
  AMD CRYPTOGRAPHIC COPROCESSOR (CCP) DRIVER
  M:    Tom Lendacky <thomas.lendacky@amd.com>
  M:    Gary Hook <gary.hook@amd.com>
@@@ -819,11 -810,11 +819,11 @@@ L:      alsa-devel@alsa-project.org (moderat
  S:    Maintained
  F:    sound/aoa/
  
 -APEX EMBEDDED SYSTEMS STX104 DAC DRIVER
 +APEX EMBEDDED SYSTEMS STX104 IIO DRIVER
  M:    William Breathitt Gray <vilhelm.gray@gmail.com>
  L:    linux-iio@vger.kernel.org
  S:    Maintained
 -F:    drivers/iio/dac/stx104.c
 +F:    drivers/iio/adc/stx104.c
  
  APM DRIVER
  M:    Jiri Kosina <jikos@kernel.org>
@@@ -866,13 -857,6 +866,13 @@@ F:       drivers/net/phy/mdio-xgene.
  F:    Documentation/devicetree/bindings/net/apm-xgene-enet.txt
  F:    Documentation/devicetree/bindings/net/apm-xgene-mdio.txt
  
 +APPLIED MICRO (APM) X-GENE SOC PMU
 +M:    Tai Nguyen <ttnguyen@apm.com>
 +S:    Supported
 +F:    drivers/perf/xgene_pmu.c
 +F:    Documentation/perf/xgene-pmu.txt
 +F:    Documentation/devicetree/bindings/perf/apm-xgene-pmu.txt
 +
  APTINA CAMERA SENSOR PLL
  M:    Laurent Pinchart <Laurent.pinchart@ideasonboard.com>
  L:    linux-media@vger.kernel.org
@@@ -929,17 -913,15 +929,17 @@@ F:      arch/arm/include/asm/floppy.
  
  ARM PMU PROFILING AND DEBUGGING
  M:    Will Deacon <will.deacon@arm.com>
 -R:    Mark Rutland <mark.rutland@arm.com>
 +M:    Mark Rutland <mark.rutland@arm.com>
  S:    Maintained
 +L:    linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
  F:    arch/arm*/kernel/perf_*
  F:    arch/arm/oprofile/common.c
  F:    arch/arm*/kernel/hw_breakpoint.c
  F:    arch/arm*/include/asm/hw_breakpoint.h
  F:    arch/arm*/include/asm/perf_event.h
 -F:    drivers/perf/arm_pmu.c
 +F:    drivers/perf/*
  F:    include/linux/perf/arm_pmu.h
 +F:    Documentation/devicetree/bindings/arm/pmu.txt
  
  ARM PORT
  M:    Russell King <linux@armlinux.org.uk>
@@@ -1010,7 -992,6 +1010,7 @@@ M:       Chen-Yu Tsai <wens@csie.org
  L:    linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
  S:    Maintained
  N:    sun[x456789]i
 +F:    arch/arm/boot/dts/ntc-gr8*
  
  ARM/Allwinner SoC Clock Support
  M:    Emilio López <emilio@elopez.com.ar>
@@@ -1142,11 -1123,6 +1142,11 @@@ F:    drivers/hwtracing/coresight/
  F:    Documentation/trace/coresight.txt
  F:    Documentation/devicetree/bindings/arm/coresight.txt
  F:    Documentation/ABI/testing/sysfs-bus-coresight-devices-*
 +F:    tools/perf/arch/arm/util/pmu.c
 +F:    tools/perf/arch/arm/util/auxtrace.c
 +F:    tools/perf/arch/arm/util/cs-etm.c
 +F:    tools/perf/arch/arm/util/cs-etm.h
 +F:    tools/perf/util/cs-etm.h
  
  ARM/CORGI MACHINE SUPPORT
  M:    Richard Purdie <rpurdie@rpsys.net>
@@@ -1467,7 -1443,6 +1467,7 @@@ F:      arch/arm/mach-orion5x/ts78xx-
  ARM/OXNAS platform support
  M:    Neil Armstrong <narmstrong@baylibre.com>
  L:    linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
 +L:    linux-oxnas@lists.tuxfamily.org (moderated for non-subscribers)
  S:    Maintained
  F:    arch/arm/mach-oxnas/
  F:    arch/arm/boot/dts/oxnas*
@@@ -1702,6 -1677,14 +1702,6 @@@ S:     Maintaine
  F:    arch/arm/plat-samsung/s5p-dev-mfc.c
  F:    drivers/media/platform/s5p-mfc/
  
 -ARM/SAMSUNG S5P SERIES TV SUBSYSTEM SUPPORT
 -M:    Kyungmin Park <kyungmin.park@samsung.com>
 -M:    Tomasz Stanislawski <t.stanislaws@samsung.com>
 -L:    linux-arm-kernel@lists.infradead.org
 -L:    linux-media@vger.kernel.org
 -S:    Maintained
 -F:    drivers/media/platform/s5p-tv/
 -
  ARM/SAMSUNG S5P SERIES HDMI CEC SUBSYSTEM SUPPORT
  M:    Kyungmin Park <kyungmin.park@samsung.com>
  L:    linux-arm-kernel@lists.infradead.org
@@@ -1857,10 -1840,8 +1857,10 @@@ F:    arch/arm/mach-uniphier
  F:    arch/arm/mm/cache-uniphier.c
  F:    arch/arm64/boot/dts/socionext/
  F:    drivers/bus/uniphier-system-bus.c
 +F:    drivers/clk/uniphier/
  F:    drivers/i2c/busses/i2c-uniphier*
  F:    drivers/pinctrl/uniphier/
 +F:    drivers/reset/reset-uniphier.c
  F:    drivers/tty/serial/8250/8250_uniphier.c
  N:    uniphier
  
@@@ -1995,13 -1976,6 +1995,13 @@@ S:    Maintaine
  F:    drivers/media/i2c/as3645a.c
  F:    include/media/i2c/as3645a.h
  
 +ASAHI KASEI AK8974 DRIVER
 +M:    Linus Walleij <linus.walleij@linaro.org>
 +L:    linux-iio@vger.kernel.org
 +W:    http://www.akm.com/
 +S:    Supported
 +F:    drivers/iio/magnetometer/ak8974.c
 +
  ASC7621 HARDWARE MONITOR DRIVER
  M:    George Joseph <george.joseph@fairview5.com>
  L:    linux-hwmon@vger.kernel.org
@@@ -2140,6 -2114,11 +2140,6 @@@ M:     Ludovic Desroches <ludovic.desroches
  S:    Maintained
  F:    drivers/mmc/host/atmel-mci.c
  
 -ATMEL AT91 / AT32 SERIAL DRIVER
 -M:    Nicolas Ferre <nicolas.ferre@atmel.com>
 -S:    Supported
 -F:    drivers/tty/serial/atmel_serial.c
 -
  ATMEL AT91 SAMA5D2-Compatible Shutdown Controller
  M:    Nicolas Ferre <nicolas.ferre@atmel.com>
  S:    Supported
@@@ -2244,9 -2223,9 +2244,9 @@@ S:      Maintaine
  F:    drivers/net/wireless/atmel/atmel*
  
  ATMEL MAXTOUCH DRIVER
 -M:    Nick Dyer <nick.dyer@itdev.co.uk>
 -T:    git git://github.com/atmel-maxtouch/linux.git
 -S:    Supported
 +M:    Nick Dyer <nick@shmanahar.org>
 +T:    git git://github.com/ndyer/linux.git
 +S:    Maintained
  F:    Documentation/devicetree/bindings/input/atmel,maxtouch.txt
  F:    drivers/input/touchscreen/atmel_mxt_ts.c
  F:    include/linux/platform_data/atmel_mxt_ts.h
@@@ -2474,7 -2453,6 +2474,7 @@@ T:      git git://git.kernel.org/pub/scm/lin
  S:    Maintained
  F:    block/
  F:    kernel/trace/blktrace.c
 +F:    lib/sbitmap.c
  
  BLOCK2MTD DRIVER
  M:    Joern Engel <joern@lazybastard.org>
@@@ -2598,13 -2576,6 +2598,13 @@@ F:    arch/arm/mach-bcm/bcm_5301x.
  F:    arch/arm/boot/dts/bcm5301x*.dtsi
  F:    arch/arm/boot/dts/bcm470*
  
 +BROADCOM BCM53573 ARM ARCHITECTURE
 +M:    Rafał Miłecki <rafal@milecki.pl>
 +L:    linux-arm-kernel@lists.infradead.org
 +S:    Maintained
 +F:    arch/arm/boot/dts/bcm53573*
 +F:    arch/arm/boot/dts/bcm47189*
 +
  BROADCOM BCM63XX ARM ARCHITECTURE
  M:    Florian Fainelli <f.fainelli@gmail.com>
  M:    bcm-kernel-feedback-list@broadcom.com
@@@ -2804,7 -2775,7 +2804,7 @@@ L:      linux-media@vger.kernel.or
  W:    https://linuxtv.org
  T:    git git://linuxtv.org/media_tree.git
  S:    Odd fixes
 -F:    Documentation/video4linux/bttv/
 +F:    Documentation/media/v4l-drivers/bttv*
  F:    drivers/media/pci/bt8xx/bttv*
  
  BUSLOGIC SCSI DRIVER
@@@ -2849,7 -2820,7 +2849,7 @@@ M:      Jonathan Corbet <corbet@lwn.net
  L:    linux-media@vger.kernel.org
  T:    git git://linuxtv.org/media_tree.git
  S:    Maintained
 -F:    Documentation/video4linux/cafe_ccic
 +F:    Documentation/media/v4l-drivers/cafe_ccic*
  F:    drivers/media/platform/marvell-ccic/
  
  CAIF NETWORK LAYER
@@@ -2918,14 -2889,6 +2918,14 @@@ S:    Maintaine
  F:    drivers/iio/light/cm*
  F:    Documentation/devicetree/bindings/i2c/trivial-devices.txt
  
 +CAVIUM I2C DRIVER
 +M:    Jan Glauber <jglauber@cavium.com>
 +M:    David Daney <david.daney@cavium.com>
 +W:    http://www.cavium.com
 +S:    Supported
 +F:    drivers/i2c/busses/i2c-octeon*
 +F:    drivers/i2c/busses/i2c-thunderx*
 +
  CAVIUM LIQUIDIO NETWORK DRIVER
  M:     Derek Chickles <derek.chickles@caviumnetworks.com>
  M:     Satanand Burla <satananda.burla@caviumnetworks.com>
@@@ -2951,7 -2914,7 +2951,7 @@@ T:      git git://linuxtv.org/media_tree.gi
  W:    http://linuxtv.org
  S:    Supported
  F:    Documentation/cec.txt
 -F:    Documentation/DocBook/media/v4l/cec*
 +F:    Documentation/media/uapi/cec
  F:    drivers/staging/media/cec/
  F:    drivers/media/cec-edid.c
  F:    drivers/media/rc/keymaps/rc-cec.c
@@@ -3173,7 -3136,7 +3173,7 @@@ L:      cocci@systeme.lip6.fr (moderated fo
  T:    git git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild.git misc
  W:    http://coccinelle.lip6.fr/
  S:    Supported
 -F:    Documentation/coccinelle.txt
 +F:    Documentation/dev-tools/coccinelle.rst
  F:    scripts/coccinelle/
  F:    scripts/coccicheck
  
@@@ -3199,7 -3162,6 +3199,7 @@@ COMMON CLK FRAMEWOR
  M:    Michael Turquette <mturquette@baylibre.com>
  M:    Stephen Boyd <sboyd@codeaurora.org>
  L:    linux-clk@vger.kernel.org
 +Q:    http://patchwork.kernel.org/project/linux-clk/list/
  T:    git git://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git
  S:    Maintained
  F:    Documentation/devicetree/bindings/clock/
@@@ -3320,7 -3282,6 +3320,7 @@@ L:      linux-pm@vger.kernel.or
  S:    Maintained
  T:    git git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git
  T:    git git://git.linaro.org/people/vireshk/linux.git (For ARM Updates)
 +F:    Documentation/cpu-freq/
  F:    drivers/cpufreq/
  F:    include/linux/cpufreq.h
  
@@@ -3439,7 -3400,7 +3439,7 @@@ T:      git git://linuxtv.org/media_tree.gi
  W:    https://linuxtv.org
  W:    http://www.ivtvdriver.org/index.php/Cx18
  S:    Maintained
 -F:    Documentation/video4linux/cx18.txt
 +F:    Documentation/media/v4l-drivers/cx18*
  F:    drivers/media/pci/cx18/
  F:    include/uapi/linux/ivtv*
  
@@@ -3468,7 -3429,7 +3468,7 @@@ L:      linux-media@vger.kernel.or
  W:    https://linuxtv.org
  T:    git git://linuxtv.org/media_tree.git
  S:    Odd fixes
 -F:    Documentation/video4linux/cx88/
 +F:    Documentation/media/v4l-drivers/cx88*
  F:    drivers/media/pci/cx88/
  
  CXD2820R MEDIA DRIVER
@@@ -3501,7 -3462,6 +3501,7 @@@ L:      linux-rdma@vger.kernel.or
  W:    http://www.openfabrics.org
  S:    Supported
  F:    drivers/infiniband/hw/cxgb3/
 +F:    include/uapi/rdma/cxgb3-abi.h
  
  CXGB4 ETHERNET DRIVER (CXGB4)
  M:    Hariprasad S <hariprasad@chelsio.com>
@@@ -3523,7 -3483,6 +3523,7 @@@ L:      linux-rdma@vger.kernel.or
  W:    http://www.openfabrics.org
  S:    Supported
  F:    drivers/infiniband/hw/cxgb4/
 +F:    include/uapi/rdma/cxgb4-abi.h
  
  CXGB4VF ETHERNET DRIVER (CXGB4VF)
  M:    Casey Leedom <leedom@chelsio.com>
@@@ -3534,14 -3493,14 +3534,14 @@@ F:   drivers/net/ethernet/chelsio/cxgb4vf
  
  CXL (IBM Coherent Accelerator Processor Interface CAPI) DRIVER
  M:    Ian Munsie <imunsie@au1.ibm.com>
 -M:    Michael Neuling <mikey@neuling.org>
 +M:    Frederic Barrat <fbarrat@linux.vnet.ibm.com>
  L:    linuxppc-dev@lists.ozlabs.org
  S:    Supported
 +F:    arch/powerpc/platforms/powernv/pci-cxl.c
  F:    drivers/misc/cxl/
  F:    include/misc/cxl*
  F:    include/uapi/misc/cxl.h
  F:    Documentation/powerpc/cxl.txt
 -F:    Documentation/powerpc/cxl.txt
  F:    Documentation/ABI/testing/sysfs-class-cxl
  
  CXLFLASH (IBM Coherent Accelerator Processor Interface CAPI Flash) SCSI DRIVER
@@@ -3804,8 -3763,8 +3804,8 @@@ F:      drivers/leds/leds-da90??.
  F:    drivers/mfd/da903x.c
  F:    drivers/mfd/da90??-*.c
  F:    drivers/mfd/da91??-*.c
 -F:    drivers/power/da9052-battery.c
 -F:    drivers/power/da91??-*.c
 +F:    drivers/power/supply/da9052-battery.c
 +F:    drivers/power/supply/da91??-*.c
  F:    drivers/regulator/da903x.c
  F:    drivers/regulator/da9???-regulator.[ch]
  F:    drivers/rtc/rtc-da90??.c
@@@ -3821,12 -3780,6 +3821,12 @@@ F:    include/linux/regulator/da9211.
  F:    include/sound/da[79]*.h
  F:    sound/soc/codecs/da[79]*.[ch]
  
 +DIAMOND SYSTEMS GPIO-MM GPIO DRIVER
 +M:    William Breathitt Gray <vilhelm.gray@gmail.com>
 +L:    linux-gpio@vger.kernel.org
 +S:    Maintained
 +F:    drivers/gpio/gpio-gpio-mm.c
 +
  DIGI NEO AND CLASSIC PCI PRODUCTS
  M:    Lidza Louina <lidza.louina@gmail.com>
  M:    Mark Hounschell <markh@compro.net>
@@@ -3949,7 -3902,7 +3949,7 @@@ X:      Documentation/devicetree
  X:    Documentation/acpi
  X:    Documentation/power
  X:    Documentation/spi
 -X:    Documentation/DocBook/media
 +X:    Documentation/media
  T:    git git://git.lwn.net/linux.git docs-next
  
  DOUBLETALK DRIVER
@@@ -4122,6 -4075,14 +4122,14 @@@ S:    Orphan / Obsolet
  F:    drivers/gpu/drm/i810/
  F:    include/uapi/drm/i810_drm.h
  
+ DRM DRIVERS FOR MEDIATEK
+ M:    CK Hu <ck.hu@mediatek.com>
+ M:    Philipp Zabel <p.zabel@pengutronix.de>
+ L:    dri-devel@lists.freedesktop.org
+ S:    Supported
+ F:    drivers/gpu/drm/mediatek/
+ F:    Documentation/devicetree/bindings/display/mediatek/
  DRM DRIVER FOR MSM ADRENO GPU
  M:    Rob Clark <robdclark@gmail.com>
  L:    linux-arm-msm@vger.kernel.org
@@@ -4452,6 -4413,7 +4460,6 @@@ F:      Documentation/filesystems/ecryptfs.t
  F:    fs/ecryptfs/
  
  EDAC-CORE
 -M:    Doug Thompson <dougthompson@xmission.com>
  M:    Borislav Petkov <bp@alien8.de>
  M:    Mauro Carvalho Chehab <mchehab@s-opensource.com>
  M:    Mauro Carvalho Chehab <mchehab@kernel.org>
@@@ -4464,12 -4426,14 +4472,12 @@@ F:   drivers/edac
  F:    include/linux/edac.h
  
  EDAC-AMD64
 -M:    Doug Thompson <dougthompson@xmission.com>
  M:    Borislav Petkov <bp@alien8.de>
  L:    linux-edac@vger.kernel.org
  S:    Maintained
  F:    drivers/edac/amd64_edac*
  
  EDAC-CALXEDA
 -M:    Doug Thompson <dougthompson@xmission.com>
  M:    Robert Richter <rric@kernel.org>
  L:    linux-edac@vger.kernel.org
  S:    Maintained
@@@ -4485,21 -4449,17 +4493,21 @@@ F:   drivers/edac/octeon_edac
  
  EDAC-E752X
  M:    Mark Gross <mark.gross@intel.com>
 -M:    Doug Thompson <dougthompson@xmission.com>
  L:    linux-edac@vger.kernel.org
  S:    Maintained
  F:    drivers/edac/e752x_edac.c
  
  EDAC-E7XXX
 -M:    Doug Thompson <dougthompson@xmission.com>
  L:    linux-edac@vger.kernel.org
  S:    Maintained
  F:    drivers/edac/e7xxx_edac.c
  
 +EDAC-FSL_DDR
 +M:    York Sun <york.sun@nxp.com>
 +L:    linux-edac@vger.kernel.org
 +S:    Maintained
 +F:    drivers/edac/fsl_ddr_edac.*
 +
  EDAC-GHES
  M:    Mauro Carvalho Chehab <mchehab@s-opensource.com>
  M:    Mauro Carvalho Chehab <mchehab@kernel.org>
@@@ -4514,11 -4474,13 +4522,11 @@@ S:   Maintaine
  F:    drivers/edac/i82443bxgx_edac.c
  
  EDAC-I3000
 -M:    Jason Uhlenkott <juhlenko@akamai.com>
  L:    linux-edac@vger.kernel.org
 -S:    Maintained
 +S:    Orphan
  F:    drivers/edac/i3000_edac.c
  
  EDAC-I5000
 -M:    Doug Thompson <dougthompson@xmission.com>
  L:    linux-edac@vger.kernel.org
  S:    Maintained
  F:    drivers/edac/i5000_edac.c
@@@ -4630,13 -4592,6 +4638,13 @@@ M:    Peter Jones <pjones@redhat.com
  S:    Maintained
  F:    drivers/video/fbdev/efifb.c
  
 +EFI TEST DRIVER
 +L:    linux-efi@vger.kernel.org
 +M:    Ivan Hu <ivan.hu@canonical.com>
 +M:    Matt Fleming <matt@codeblueprint.co.uk>
 +S:    Maintained
 +F:    drivers/firmware/efi/test/
 +
  EFS FILESYSTEM
  W:    http://aeschi.ch.eu.org/efs/
  S:    Orphan
@@@ -4656,7 -4611,6 +4664,7 @@@ W:      https://linuxtv.or
  T:    git git://linuxtv.org/media_tree.git
  S:    Maintained
  F:    drivers/media/usb/em28xx/
 +F:    Documentation/media/v4l-drivers/em28xx*
  
  EMBEDDED LINUX
  M:    Paul Gortmaker <paul.gortmaker@windriver.com>
@@@ -4905,7 -4859,6 +4913,7 @@@ F:      tools/firewire
  
  FIRMWARE LOADER (request_firmware)
  M:    Ming Lei <ming.lei@canonical.com>
 +M:    Luis R. Rodriguez <mcgrof@kernel.org>
  L:    linux-kernel@vger.kernel.org
  S:    Maintained
  F:    Documentation/firmware_class/
@@@ -5026,13 -4979,6 +5034,13 @@@ F:    drivers/net/ethernet/freescale/fec_p
  F:    drivers/net/ethernet/freescale/fec.h
  F:    Documentation/devicetree/bindings/net/fsl-fec.txt
  
 +FREESCALE QORIQ DPAA FMAN DRIVER
 +M:    Madalin Bucur <madalin.bucur@nxp.com>
 +L:    netdev@vger.kernel.org
 +S:    Maintained
 +F:    drivers/net/ethernet/freescale/fman
 +F:    Documentation/devicetree/bindings/powerpc/fsl/fman.txt
 +
  FREESCALE QUICC ENGINE LIBRARY
  L:    linuxppc-dev@lists.ozlabs.org
  S:    Orphan
@@@ -5133,9 -5079,10 +5141,9 @@@ F:     include/linux/fscrypto.
  
  F2FS FILE SYSTEM
  M:    Jaegeuk Kim <jaegeuk@kernel.org>
 -M:    Changman Lee <cm224.lee@samsung.com>
 -R:    Chao Yu <yuchao0@huawei.com>
 +M:    Chao Yu <yuchao0@huawei.com>
  L:    linux-f2fs-devel@lists.sourceforge.net
 -W:    http://en.wikipedia.org/wiki/F2FS
 +W:    https://f2fs.wiki.kernel.org/
  T:    git git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs.git
  S:    Maintained
  F:    Documentation/filesystems/f2fs.txt
@@@ -5197,7 -5144,7 +5205,7 @@@ GCOV BASED KERNEL PROFILIN
  M:    Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
  S:    Maintained
  F:    kernel/gcov/
 -F:    Documentation/gcov.txt
 +F:    Documentation/dev-tools/gcov.rst
  
  GDT SCSI DISK ARRAY CONTROLLER DRIVER
  M:    Achim Leubner <achim_leubner@adaptec.com>
@@@ -5314,13 -5261,6 +5322,13 @@@ L:    linux-input@vger.kernel.or
  S:    Maintained
  F:    drivers/input/touchscreen/goodix.c
  
 +GPIO MOCKUP DRIVER
 +M:    Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>
 +L:    linux-gpio@vger.kernel.org
 +S:    Maintained
 +F:    drivers/gpio/gpio-mockup.c
 +F:    tools/testing/selftests/gpio/
 +
  GPIO SUBSYSTEM
  M:    Linus Walleij <linus.walleij@linaro.org>
  M:    Alexandre Courbot <gnurou@gmail.com>
@@@ -5352,84 -5292,6 +5360,84 @@@ L:    netdev@vger.kernel.or
  S:    Maintained
  F:    drivers/net/ethernet/aeroflex/
  
 +GREYBUS SUBSYSTEM
 +M:    Johan Hovold <johan@kernel.org>
 +M:    Alex Elder <elder@kernel.org>
 +M:    Greg Kroah-Hartman <gregkh@linuxfoundation.org>
 +S:    Maintained
 +F:    drivers/staging/greybus/
 +
 +GREYBUS AUDIO PROTOCOLS DRIVERS
 +M:    Vaibhav Agarwal <vaibhav.sr@gmail.com>
 +M:    Mark Greer <mgreer@animalcreek.com>
 +S:    Maintained
 +F:    drivers/staging/greybus/audio_apbridgea.c
 +F:    drivers/staging/greybus/audio_apbridgea.h
 +F:    drivers/staging/greybus/audio_codec.c
 +F:    drivers/staging/greybus/audio_codec.h
 +F:    drivers/staging/greybus/audio_gb.c
 +F:    drivers/staging/greybus/audio_manager.c
 +F:    drivers/staging/greybus/audio_manager.h
 +F:    drivers/staging/greybus/audio_manager_module.c
 +F:    drivers/staging/greybus/audio_manager_private.h
 +F:    drivers/staging/greybus/audio_manager_sysfs.c
 +F:    drivers/staging/greybus/audio_module.c
 +F:    drivers/staging/greybus/audio_topology.c
 +
 +GREYBUS PROTOCOLS DRIVERS
 +M:    Rui Miguel Silva <rmfrfs@gmail.com>
 +S:    Maintained
 +F:    drivers/staging/greybus/sdio.c
 +F:    drivers/staging/greybus/light.c
 +F:    drivers/staging/greybus/gpio.c
 +F:    drivers/staging/greybus/power_supply.c
 +F:    drivers/staging/greybus/spi.c
 +F:    drivers/staging/greybus/spilib.c
 +
 +GREYBUS PROTOCOLS DRIVERS
 +M:    Bryan O'Donoghue <pure.logic@nexus-software.ie>
 +S:    Maintained
 +F:    drivers/staging/greybus/loopback.c
 +F:    drivers/staging/greybus/timesync.c
 +F:    drivers/staging/greybus/timesync_platform.c
 +
 +GREYBUS PROTOCOLS DRIVERS
 +M:    Viresh Kumar <vireshk@kernel.org>
 +S:    Maintained
 +F:    drivers/staging/greybus/authentication.c
 +F:    drivers/staging/greybus/bootrom.c
 +F:    drivers/staging/greybus/firmware.h
 +F:    drivers/staging/greybus/fw-core.c
 +F:    drivers/staging/greybus/fw-download.c
 +F:    drivers/staging/greybus/fw-managament.c
 +F:    drivers/staging/greybus/greybus_authentication.h
 +F:    drivers/staging/greybus/greybus_firmware.h
 +F:    drivers/staging/greybus/hid.c
 +F:    drivers/staging/greybus/i2c.c
 +F:    drivers/staging/greybus/spi.c
 +F:    drivers/staging/greybus/spilib.c
 +F:    drivers/staging/greybus/spilib.h
 +
 +GREYBUS PROTOCOLS DRIVERS
 +M:    David Lin <dtwlin@gmail.com>
 +S:    Maintained
 +F:    drivers/staging/greybus/uart.c
 +F:    drivers/staging/greybus/log.c
 +
 +GREYBUS PLATFORM DRIVERS
 +M:    Vaibhav Hiremath <hvaibhav.linux@gmail.com>
 +S:    Maintained
 +F:    drivers/staging/greybus/arche-platform.c
 +F:    drivers/staging/greybus/arche-apb-ctrl.c
 +F:    drivers/staging/greybus/arche_platform.h
 +
 +GS1662 VIDEO SERIALIZER
 +M:    Charles-Antoine Couret <charles-antoine.couret@nexvision.fr>
 +L:    linux-media@vger.kernel.org
 +T:    git git://linuxtv.org/media_tree.git
 +S:    Maintained
 +F:    drivers/media/spi/gs1662.c
 +
  GSPCA FINEPIX SUBDRIVER
  M:    Frank Zago <frank@zago.net>
  L:    linux-media@vger.kernel.org
@@@ -5721,14 -5583,6 +5729,14 @@@ S:    Maintaine
  F:    drivers/net/ethernet/hisilicon/
  F:    Documentation/devicetree/bindings/net/hisilicon*.txt
  
 +HISILICON ROCE DRIVER
 +M:    Lijun Ou <oulijun@huawei.com>
 +M:    Wei Hu(Xavier) <xavier.huwei@huawei.com>
 +L:    linux-rdma@vger.kernel.org
 +S:    Maintained
 +F:    drivers/infiniband/hw/hns/
 +F:    Documentation/devicetree/bindings/infiniband/hisilicon-hns-roce.txt
 +
  HISILICON SAS Controller
  M:    John Garry <john.garry@huawei.com>
  W:    http://www.hisilicon.com
@@@ -5738,9 -5592,10 +5746,9 @@@ F:     Documentation/devicetree/bindings/sc
  
  HOST AP DRIVER
  M:    Jouni Malinen <j@w1.fi>
 -L:    hostap@shmoo.com (subscribers-only)
  L:    linux-wireless@vger.kernel.org
 -W:    http://hostap.epitest.fi/
 -S:    Maintained
 +W:    http://w1.fi/hostap-driver.html
 +S:    Obsolete
  F:    drivers/net/wireless/intersil/hostap/
  
  HP COMPAQ TC1100 TABLET WMI EXTRAS DRIVER
@@@ -5777,7 -5632,7 +5785,7 @@@ M:      Sebastian Reichel <sre@kernel.org
  T:    git git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-hsi.git
  S:    Maintained
  F:    Documentation/ABI/testing/sysfs-bus-hsi
 -F:    Documentation/hsi.txt
 +F:    Documentation/device-drivers/serial-interfaces.rst
  F:    drivers/hsi/
  F:    include/linux/hsi/
  F:    include/uapi/linux/hsi/
@@@ -5805,14 -5660,6 +5813,14 @@@ M:    Nadia Yvette Chambers <nyc@holomorph
  S:    Maintained
  F:    fs/hugetlbfs/
  
 +HVA ST MEDIA DRIVER
 +M:    Jean-Christophe Trotin <jean-christophe.trotin@st.com>
 +L:    linux-media@vger.kernel.org
 +T:    git git://linuxtv.org/media_tree.git
 +W:    https://linuxtv.org
 +S:    Supported
 +F:    drivers/media/platform/sti/hva
 +
  Hyper-V CORE AND DRIVERS
  M:    "K. Y. Srinivasan" <kys@microsoft.com>
  M:    Haiyang Zhang <haiyangz@microsoft.com>
@@@ -5839,8 -5686,6 +5847,8 @@@ S:      Maintaine
  F:    Documentation/i2c/i2c-topology
  F:    Documentation/i2c/muxes/
  F:    Documentation/devicetree/bindings/i2c/i2c-mux*
 +F:    Documentation/devicetree/bindings/i2c/i2c-arb*
 +F:    Documentation/devicetree/bindings/i2c/i2c-gate*
  F:    drivers/i2c/i2c-mux.c
  F:    drivers/i2c/muxes/
  F:    include/linux/i2c-mux.h
@@@ -6157,12 -6002,6 +6165,12 @@@ M:    Zubair Lutfullah Kakakhel <Zubair.Ka
  S:    Maintained
  F:    drivers/dma/dma-jz4780.c
  
 +INGENIC JZ4780 NAND DRIVER
 +M:    Harvey Hunt <harveyhuntnexus@gmail.com>
 +L:    linux-mtd@lists.infradead.org
 +S:    Maintained
 +F:    drivers/mtd/nand/jz4780_*
 +
  INTEGRITY MEASUREMENT ARCHITECTURE (IMA)
  M:    Mimi Zohar <zohar@linux.vnet.ibm.com>
  M:    Dmitry Kasatkin <dmitry.kasatkin@gmail.com>
@@@ -6264,13 -6103,6 +6272,13 @@@ T:    git git://git.kernel.org/pub/scm/lin
  S:    Supported
  F:    drivers/idle/intel_idle.c
  
 +INTEL INTEGRATED SENSOR HUB DRIVER
 +M:    Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
 +M:    Jiri Kosina <jikos@kernel.org>
 +L:    linux-input@vger.kernel.org
 +S:    Maintained
 +F:    drivers/hid/intel-ish-hid/
 +
  INTEL PSTATE DRIVER
  M:    Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
  M:    Len Brown <lenb@kernel.org>
@@@ -6697,7 -6529,7 +6705,7 @@@ L:      linux-media@vger.kernel.or
  T:    git git://linuxtv.org/media_tree.git
  W:    http://www.ivtvdriver.org
  S:    Maintained
 -F:    Documentation/video4linux/*.ivtv
 +F:    Documentation/media/v4l-drivers/ivtv*
  F:    drivers/media/pci/ivtv/
  F:    include/uapi/linux/ivtv*
  
@@@ -6781,7 -6613,7 +6789,7 @@@ L:      kasan-dev@googlegroups.co
  S:    Maintained
  F:    arch/*/include/asm/kasan.h
  F:    arch/*/mm/kasan_init*
 -F:    Documentation/kasan.txt
 +F:    Documentation/dev-tools/kasan.rst
  F:    include/linux/kasan*.h
  F:    lib/test_kasan.c
  F:    mm/kasan/
@@@ -6997,7 -6829,7 +7005,7 @@@ KMEMCHEC
  M:    Vegard Nossum <vegardno@ifi.uio.no>
  M:    Pekka Enberg <penberg@kernel.org>
  S:    Maintained
 -F:    Documentation/kmemcheck.txt
 +F:    Documentation/dev-tools/kmemcheck.rst
  F:    arch/x86/include/asm/kmemcheck.h
  F:    arch/x86/mm/kmemcheck/
  F:    include/linux/kmemcheck.h
@@@ -7006,7 -6838,7 +7014,7 @@@ F:      mm/kmemcheck.
  KMEMLEAK
  M:    Catalin Marinas <catalin.marinas@arm.com>
  S:    Maintained
 -F:    Documentation/kmemleak.txt
 +F:    Documentation/dev-tools/kmemleak.rst
  F:    include/linux/kmemleak.h
  F:    mm/kmemleak.c
  F:    mm/kmemleak-test.c
@@@ -7619,8 -7451,9 +7627,8 @@@ F:      Documentation/hwmon/max2075
  F:    drivers/hwmon/max20751.c
  
  MAX6650 HARDWARE MONITOR AND FAN CONTROLLER DRIVER
 -M:    "Hans J. Koch" <hjk@hansjkoch.de>
  L:    linux-hwmon@vger.kernel.org
 -S:    Maintained
 +S:    Orphan
  F:    Documentation/hwmon/max6650
  F:    drivers/hwmon/max6650.c
  
@@@ -7645,8 -7478,8 +7653,8 @@@ M:      Krzysztof Kozlowski <krzk@kernel.org
  M:    Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>
  L:    linux-pm@vger.kernel.org
  S:    Supported
 -F:    drivers/power/max14577_charger.c
 -F:    drivers/power/max77693_charger.c
 +F:    drivers/power/supply/max14577_charger.c
 +F:    drivers/power/supply/max77693_charger.c
  
  MAXIM MAX77802 MULTIFUNCTION PMIC DEVICE DRIVERS
  M:    Javier Martinez Canillas <javier@osg.samsung.com>
@@@ -7691,12 -7524,6 +7699,12 @@@ L:    linux-iio@vger.kernel.or
  S:    Maintained
  F:    drivers/iio/potentiometer/mcp4531.c
  
 +MEASUREMENT COMPUTING CIO-DAC IIO DRIVER
 +M:    William Breathitt Gray <vilhelm.gray@gmail.com>
 +L:    linux-iio@vger.kernel.org
 +S:    Maintained
 +F:    drivers/iio/dac/cio-dac.c
 +
  MEDIA DRIVERS FOR RENESAS - FCP
  M:    Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  L:    linux-media@vger.kernel.org
@@@ -7707,15 -7534,6 +7715,15 @@@ F:    Documentation/devicetree/bindings/me
  F:    drivers/media/platform/rcar-fcp.c
  F:    include/media/rcar-fcp.h
  
 +MEDIA DRIVERS FOR RENESAS - VIN
 +M:    Niklas Söderlund <niklas.soderlund@ragnatech.se>
 +L:    linux-media@vger.kernel.org
 +L:    linux-renesas-soc@vger.kernel.org
 +T:    git git://linuxtv.org/media_tree.git
 +S:    Supported
 +F:    Documentation/devicetree/bindings/media/rcar_vin.txt
 +F:    drivers/media/platform/rcar-vin/
 +
  MEDIA DRIVERS FOR RENESAS - VSP1
  M:    Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  L:    linux-media@vger.kernel.org
@@@ -7793,7 -7611,9 +7801,7 @@@ W:      https://linuxtv.or
  Q:    http://patchwork.kernel.org/project/linux-media/list/
  T:    git git://linuxtv.org/media_tree.git
  S:    Maintained
 -F:    Documentation/dvb/
 -F:    Documentation/video4linux/
 -F:    Documentation/DocBook/media/
 +F:    Documentation/media/
  F:    drivers/media/
  F:    drivers/staging/media/
  F:    include/linux/platform_data/media/
@@@ -7856,19 -7676,6 +7864,19 @@@ W:    http://www.mellanox.co
  Q:    http://patchwork.ozlabs.org/project/netdev/list/
  F:    drivers/net/ethernet/mellanox/mlxsw/
  
 +MELLANOX MLXCPLD LED DRIVER
 +M:    Vadim Pasternak <vadimp@mellanox.com>
 +L:    linux-leds@vger.kernel.org
 +S:    Supported
 +F:    drivers/leds/leds-mlxcpld.c
 +F:    Documentation/leds/leds-mlxcpld.txt
 +
 +MELLANOX PLATFORM DRIVER
 +M:      Vadim Pasternak <vadimp@mellanox.com>
 +L:      platform-driver-x86@vger.kernel.org
 +S:      Supported
 +F:      arch/x86/platform/mellanox/mlx-platform.c
 +
  SOFT-ROCE DRIVER (rxe)
  M:    Moni Shoua <monis@mellanox.com>
  L:    linux-rdma@vger.kernel.org
@@@ -7954,20 -7761,6 +7962,20 @@@ T:    git git://git.monstr.eu/linux-2.6-mi
  S:    Supported
  F:    arch/microblaze/
  
 +MICROCHIP / ATMEL AT91 / AT32 SERIAL DRIVER
 +M:    Richard Genoud <richard.genoud@gmail.com>
 +S:    Maintained
 +F:    drivers/tty/serial/atmel_serial.c
 +F:    include/linux/atmel_serial.h
 +
 +MICROCHIP / ATMEL ISC DRIVER
 +M:    Songjun Wu <songjun.wu@microchip.com>
 +L:    linux-media@vger.kernel.org
 +S:    Supported
 +F:    drivers/media/platform/atmel/atmel-isc.c
 +F:    drivers/media/platform/atmel/atmel-isc-regs.h
 +F:    devicetree/bindings/media/atmel-isc.txt
 +
  MICROSOFT SURFACE PRO 3 BUTTON DRIVER
  M:    Chen Yu <yu.c.chen@intel.com>
  L:    platform-driver-x86@vger.kernel.org
@@@ -8016,7 -7809,6 +8024,7 @@@ Q:      http://patchwork.ozlabs.org/project/
  S:    Supported
  F:    drivers/net/ethernet/mellanox/mlx4/
  F:    include/linux/mlx4/
 +F:    include/uapi/rdma/mlx4-abi.h
  
  MELLANOX MLX4 IB driver
  M:    Yishai Hadas <yishaih@mellanox.com>
@@@ -8037,7 -7829,6 +8045,7 @@@ Q:      http://patchwork.ozlabs.org/project/
  S:    Supported
  F:    drivers/net/ethernet/mellanox/mlx5/core/
  F:    include/linux/mlx5/
 +F:    include/uapi/rdma/mlx5-abi.h
  
  MELLANOX MLX5 IB driver
  M:    Matan Barak <matanb@mellanox.com>
@@@ -8056,18 -7847,6 +8064,18 @@@ W:    http://www.melexis.co
  S:    Supported
  F:    drivers/iio/temperature/mlx90614.c
  
 +MICROSEMI SMART ARRAY SMARTPQI DRIVER (smartpqi)
 +M:    Don Brace <don.brace@microsemi.com>
 +L:    esc.storagedev@microsemi.com
 +L:    linux-scsi@vger.kernel.org
 +S:    Supported
 +F:    drivers/scsi/smartpqi/smartpqi*.[ch]
 +F:    drivers/scsi/smartpqi/Kconfig
 +F:    drivers/scsi/smartpqi/Makefile
 +F:    include/linux/cciss*.h
 +F:    include/uapi/linux/cciss*.h
 +F:    Documentation/scsi/smartpqi.txt
 +
  MN88472 MEDIA DRIVER
  M:    Antti Palosaari <crope@iki.fi>
  L:    linux-media@vger.kernel.org
@@@ -8095,7 -7874,7 +8103,7 @@@ F:      kernel/module.
  MOTION EYE VAIO PICTUREBOOK CAMERA DRIVER
  W:    http://popies.net/meye/
  S:    Orphan
 -F:    Documentation/video4linux/meye.txt
 +F:    Documentation/media/v4l-drivers/meye*
  F:    drivers/media/pci/meye/
  F:    include/uapi/linux/meye.h
  
@@@ -8193,7 -7972,6 +8201,7 @@@ MULTIFUNCTION DEVICES (MFD
  M:    Lee Jones <lee.jones@linaro.org>
  T:    git git://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd.git
  S:    Supported
 +F:    Documentation/devicetree/bindings/mfd/
  F:    drivers/mfd/
  F:    include/linux/mfd/
  
@@@ -8280,16 -8058,20 +8288,16 @@@ M:   Michael Schmitz <schmitzmic@gmail.co
  L:    linux-scsi@vger.kernel.org
  S:    Maintained
  F:    Documentation/scsi/g_NCR5380.txt
 -F:    Documentation/scsi/dtc3x80.txt
  F:    drivers/scsi/NCR5380.*
  F:    drivers/scsi/arm/cumana_1.c
  F:    drivers/scsi/arm/oak.c
  F:    drivers/scsi/atari_scsi.*
  F:    drivers/scsi/dmx3191d.c
 -F:    drivers/scsi/dtc.*
  F:    drivers/scsi/g_NCR5380.*
  F:    drivers/scsi/g_NCR5380_mmio.c
  F:    drivers/scsi/mac_scsi.*
 -F:    drivers/scsi/pas16.*
  F:    drivers/scsi/sun3_scsi.*
  F:    drivers/scsi/sun3_scsi_vme.c
 -F:    drivers/scsi/t128.*
  
  NCR DUAL 700 SCSI DRIVER (MICROCHANNEL)
  M:    "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>
@@@ -8310,7 -8092,6 +8318,7 @@@ L:      linux-rdma@vger.kernel.or
  W:    http://www.intel.com/Products/Server/Adapters/Server-Cluster/Server-Cluster-overview.htm
  S:    Supported
  F:    drivers/infiniband/hw/nes/
 +F:    include/uapi/rdma/nes-abi.h
  
  NETEM NETWORK EMULATOR
  M:    Stephen Hemminger <stephen@networkplumber.org>
@@@ -8579,11 -8360,11 +8587,11 @@@ R:   Pali Rohár <pali.rohar@gmail.com
  F:    include/linux/power/bq2415x_charger.h
  F:    include/linux/power/bq27xxx_battery.h
  F:    include/linux/power/isp1704_charger.h
 -F:    drivers/power/bq2415x_charger.c
 -F:    drivers/power/bq27xxx_battery.c
 -F:    drivers/power/bq27xxx_battery_i2c.c
 -F:    drivers/power/isp1704_charger.c
 -F:    drivers/power/rx51_battery.c
 +F:    drivers/power/supply/bq2415x_charger.c
 +F:    drivers/power/supply/bq27xxx_battery.c
 +F:    drivers/power/supply/bq27xxx_battery_i2c.c
 +F:    drivers/power/supply/isp1704_charger.c
 +F:    drivers/power/supply/rx51_battery.c
  
  NTB DRIVER CORE
  M:    Jon Mason <jdmason@kudzu.us>
@@@ -8972,7 -8753,7 +8980,7 @@@ F:      drivers/oprofile
  F:    include/linux/oprofile.h
  
  ORACLE CLUSTER FILESYSTEM 2 (OCFS2)
 -M:    Mark Fasheh <mfasheh@suse.com>
 +M:    Mark Fasheh <mfasheh@versity.com>
  M:    Joel Becker <jlbec@evilplan.org>
  L:    ocfs2-devel@oss.oracle.com (moderated for non-subscribers)
  W:    http://ocfs2.wiki.kernel.org
@@@ -9084,7 -8865,6 +9092,7 @@@ S:      Supporte
  F:    Documentation/virtual/paravirt_ops.txt
  F:    arch/*/kernel/paravirt*
  F:    arch/*/include/asm/paravirt.h
 +F:    include/linux/hypervisor.h
  
  PARIDE DRIVERS FOR PARALLEL PORT IDE DEVICES
  M:    Tim Waugh <tim@cyberelk.net>
@@@ -9340,15 -9120,6 +9348,15 @@@ S:    Maintaine
  F:    Documentation/devicetree/bindings/pci/hisilicon-pcie.txt
  F:    drivers/pci/host/pcie-hisi.c
  
 +PCIE DRIVER FOR ROCKCHIP
 +M:    Shawn Lin <shawn.lin@rock-chips.com>
 +M:    Wenrui Li <wenrui.li@rock-chips.com>
 +L:    linux-pci@vger.kernel.org
 +L:    linux-rockchip@lists.infradead.org
 +S:    Maintained
 +F:    Documentation/devicetree/bindings/pci/rockchip-pcie.txt
 +F:    drivers/pci/host/pcie-rockchip.c
 +
  PCIE DRIVER FOR QUALCOMM MSM
  M:     Stanimir Varbanov <svarbanov@mm-sol.com>
  L:     linux-pci@vger.kernel.org
@@@ -9502,8 -9273,6 +9510,8 @@@ L:      linux-arm-kernel@lists.infradead.or
  L:    linux-samsung-soc@vger.kernel.org (moderated for non-subscribers)
  S:    Maintained
  F:    drivers/pinctrl/samsung/
 +F:    include/dt-bindings/pinctrl/samsung.h
 +F:    Documentation/devicetree/bindings/pinctrl/samsung-pinctrl.txt
  
  PIN CONTROLLER - SINGLE
  M:    Tony Lindgren <tony@atomide.com>
@@@ -9594,12 -9363,16 +9602,12 @@@ F:   drivers/powercap
  
  POWER SUPPLY CLASS/SUBSYSTEM and DRIVERS
  M:    Sebastian Reichel <sre@kernel.org>
 -M:    Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
 -M:    David Woodhouse <dwmw2@infradead.org>
  L:    linux-pm@vger.kernel.org
 -T:    git git://git.infradead.org/battery-2.6.git
 +T:    git git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-power-supply.git
  S:    Maintained
 -F:    Documentation/devicetree/bindings/power/
 -F:    Documentation/devicetree/bindings/power_supply/
 +F:    Documentation/devicetree/bindings/power/supply/
  F:    include/linux/power_supply.h
 -F:    drivers/power/
 -X:    drivers/power/avs/
 +F:    drivers/power/supply/
  
  POWER STATE COORDINATION INTERFACE (PSCI)
  M:    Mark Rutland <mark.rutland@arm.com>
@@@ -9754,7 -9527,7 +9762,7 @@@ L:      linux-media@vger.kernel.or
  W:    http://www.isely.net/pvrusb2/
  T:    git git://linuxtv.org/media_tree.git
  S:    Maintained
 -F:    Documentation/video4linux/README.pvrusb2
 +F:    Documentation/media/v4l-drivers/pvrusb2*
  F:    drivers/media/usb/pvrusb2/
  
  PWC WEBCAM DRIVER
@@@ -9935,12 -9708,6 +9943,12 @@@ T:    git git://git.kernel.org/pub/scm/lin
  S:    Supported
  F:    drivers/net/wireless/ath/ath10k/
  
 +QUALCOMM EMAC GIGABIT ETHERNET DRIVER
 +M:    Timur Tabi <timur@codeaurora.org>
 +L:    netdev@vger.kernel.org
 +S:    Supported
 +F:    drivers/net/ethernet/qualcomm/emac/
 +
  QUALCOMM HEXAGON ARCHITECTURE
  M:    Richard Kuo <rkuo@codeaurora.org>
  L:    linux-hexagon@vger.kernel.org
@@@ -10161,12 -9928,6 +10169,12 @@@ F:  drivers/rpmsg
  F:    Documentation/rpmsg.txt
  F:    include/linux/rpmsg.h
  
 +RENESAS CLOCK DRIVERS
 +M:    Geert Uytterhoeven <geert+renesas@glider.be>
 +L:    linux-renesas-soc@vger.kernel.org
 +S:    Supported
 +F:    drivers/clk/renesas/
 +
  RENESAS ETHERNET DRIVERS
  R:    Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>
  L:    netdev@vger.kernel.org
@@@ -10202,7 -9963,6 +10210,7 @@@ F:    net/rfkill
  
  RHASHTABLE
  M:    Thomas Graf <tgraf@suug.ch>
 +M:    Herbert Xu <herbert@gondor.apana.org.au>
  L:    netdev@vger.kernel.org
  S:    Maintained
  F:    lib/rhashtable.c
@@@ -10346,8 -10106,8 +10354,8 @@@ S:   Supporte
  F:    drivers/s390/cio/
  
  S390 DASD DRIVER
 -M:    Stefan Weinhuber <wein@de.ibm.com>
 -M:    Stefan Haberland <stefan.haberland@de.ibm.com>
 +M:    Stefan Haberland <sth@linux.vnet.ibm.com>
 +M:    Jan Hoeppner <hoeppner@linux.vnet.ibm.com>
  L:    linux-s390@vger.kernel.org
  W:    http://www.ibm.com/developerworks/linux/linux390/
  S:    Supported
@@@ -10421,7 -10181,7 +10429,7 @@@ L:   linux-media@vger.kernel.or
  W:    https://linuxtv.org
  T:    git git://linuxtv.org/media_tree.git
  S:    Odd fixes
 -F:    Documentation/video4linux/*.saa7134
 +F:    Documentation/media/v4l-drivers/saa7134*
  F:    drivers/media/pci/saa7134/
  
  SAA7146 VIDEO4LINUX-2 DRIVER
@@@ -10511,12 -10271,9 +10519,12 @@@ F: drivers/nfc/s3fwrn
  SAMSUNG SOC CLOCK DRIVERS
  M:    Sylwester Nawrocki <s.nawrocki@samsung.com>
  M:    Tomasz Figa <tomasz.figa@gmail.com>
 +M:    Chanwoo Choi <cw00.choi@samsung.com>
  S:    Supported
  L:    linux-samsung-soc@vger.kernel.org (moderated for non-subscribers)
  F:    drivers/clk/samsung/
 +F:    include/dt-bindings/clock/exynos*.h
 +F:    Documentation/devicetree/bindings/clock/exynos*.txt
  
  SAMSUNG SPI DRIVERS
  M:    Kukjin Kim <kgene@kernel.org>
@@@ -10566,13 -10323,6 +10574,13 @@@ S: Maintaine
  F:    Documentation/devicetree/bindings/serial/
  F:    drivers/tty/serial/
  
 +STI CEC DRIVER
 +M:    Benjamin Gaignard <benjamin.gaignard@linaro.org>
 +L:    kernel@stlinux.com
 +S:    Maintained
 +F:    drivers/staging/media/st-cec/
 +F:    Documentation/devicetree/bindings/media/stih-cec.txt
 +
  SYNOPSYS DESIGNWARE DMAC DRIVER
  M:    Viresh Kumar <vireshk@kernel.org>
  M:    Andy Shevchenko <andriy.shevchenko@linux.intel.com>
@@@ -10621,8 -10371,8 +10629,8 @@@ F:   drivers/thunderbolt
  TI BQ27XXX POWER SUPPLY DRIVER
  R:    Andrew F. Davis <afd@ti.com>
  F:    include/linux/power/bq27xxx_battery.h
 -F:    drivers/power/bq27xxx_battery.c
 -F:    drivers/power/bq27xxx_battery_i2c.c
 +F:    drivers/power/supply/bq27xxx_battery.c
 +F:    drivers/power/supply/bq27xxx_battery_i2c.c
  
  TIMEKEEPING, CLOCKSOURCE CORE, NTP, ALARMTIMER
  M:    John Stultz <john.stultz@linaro.org>
@@@ -10850,12 -10600,12 +10858,12 @@@ S:        Maintaine
  F:    drivers/misc/phantom.c
  F:    include/uapi/linux/phantom.h
  
 -SERVER ENGINES 10Gbps iSCSI - BladeEngine 2 DRIVER
 -M:    Jayamohan Kallickal <jayamohan.kallickal@avagotech.com>
 -M:    Ketan Mukadam <ketan.mukadam@avagotech.com>
 -M:    John Soni Jose <sony.john@avagotech.com>
 +Emulex 10Gbps iSCSI - OneConnect DRIVER
 +M:    Subbu Seetharaman <subbu.seetharaman@broadcom.com>
 +M:    Ketan Mukadam <ketan.mukadam@broadcom.com>
 +M:    Jitendra Bhivare <jitendra.bhivare@broadcom.com>
  L:    linux-scsi@vger.kernel.org
 -W:    http://www.avagotech.com
 +W:    http://www.broadcom.com
  S:    Supported
  F:    drivers/scsi/be2iscsi/
  
@@@ -10877,7 -10627,6 +10885,7 @@@ L:   linux-rdma@vger.kernel.or
  W:    http://www.emulex.com
  S:    Supported
  F:    drivers/infiniband/hw/ocrdma/
 +F:    include/uapi/rdma/ocrdma-abi.h
  
  SFC NETWORK DRIVER
  M:    Solarflare linux maintainers <linux-net-drivers@solarflare.com>
@@@ -11383,7 -11132,6 +11391,7 @@@ F:   Documentation/spi
  F:    drivers/spi/
  F:    include/linux/spi/
  F:    include/uapi/linux/spi/
 +F:    tools/spi/
  
  SPIDERNET NETWORK DRIVER for CELL
  M:    Ishizaki Kou <kou.ishizaki@toshiba.co.jp>
@@@ -11454,7 -11202,6 +11462,7 @@@ F:   drivers/staging/media/lirc
  STAGING - LUSTRE PARALLEL FILESYSTEM
  M:    Oleg Drokin <oleg.drokin@intel.com>
  M:    Andreas Dilger <andreas.dilger@intel.com>
 +M:    James Simmons <jsimmons@infradead.org>
  L:    lustre-devel@lists.lustre.org (moderated for non-subscribers)
  W:    http://wiki.lustre.org/
  S:    Maintained
@@@ -11481,6 -11228,13 +11489,6 @@@ M:  Florian Schilhabel <florian.c.schilh
  S:    Odd Fixes
  F:    drivers/staging/rtl8712/
  
 -STAGING - REALTEK RTL8723U WIRELESS DRIVER
 -M:    Larry Finger <Larry.Finger@lwfinger.net>
 -M:    Jes Sorensen <Jes.Sorensen@redhat.com>
 -L:    linux-wireless@vger.kernel.org
 -S:    Maintained
 -F:    drivers/staging/rtl8723au/
 -
  STAGING - SILICON MOTION SM750 FRAME BUFFER DRIVER
  M:    Sudip Mukherjee <sudipm.mukherjee@gmail.com>
  M:    Teddy Wang <teddy.wang@siliconmotion.com>
@@@ -11620,14 -11374,6 +11628,14 @@@ T: git git://git.kernel.org/pub/scm/lin
  S:    Supported
  F:    drivers/mfd/syscon.c
  
 +SYSTEM RESET/SHUTDOWN DRIVERS
 +M:    Sebastian Reichel <sre@kernel.org>
 +L:    linux-pm@vger.kernel.org
 +T:    git git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-power-supply.git
 +S:    Maintained
 +F:    Documentation/devicetree/bindings/power/reset/
 +F:    drivers/power/reset/
 +
  SYSV FILESYSTEM
  M:    Christoph Hellwig <hch@infradead.org>
  S:    Maintained
@@@ -11888,7 -11634,7 +11896,7 @@@ F:   Documentation/devicetree/bindings/th
  THERMAL/CPU_COOLING
  M:    Amit Daniel Kachhap <amit.kachhap@gmail.com>
  M:    Viresh Kumar <viresh.kumar@linaro.org>
 -M:    Javi Merino <javi.merino@arm.com>
 +M:    Javi Merino <javi.merino@kernel.org>
  L:    linux-pm@vger.kernel.org
  S:    Supported
  F:    Documentation/thermal/cpu-cooling-api.txt
@@@ -11976,7 -11722,7 +11984,7 @@@ F:   include/linux/platform_data/lp855x.
  TI LP8727 CHARGER DRIVER
  M:    Milo Kim <milo.kim@ti.com>
  S:    Maintained
 -F:    drivers/power/lp8727_charger.c
 +F:    drivers/power/supply/lp8727_charger.c
  F:    include/linux/platform_data/lp8727.h
  
  TI LP8788 MFD DRIVER
@@@ -11985,7 -11731,7 +11993,7 @@@ S:   Maintaine
  F:    drivers/iio/adc/lp8788_adc.c
  F:    drivers/leds/leds-lp8788.c
  F:    drivers/mfd/lp8788*.c
 -F:    drivers/power/lp8788-charger.c
 +F:    drivers/power/supply/lp8788-charger.c
  F:    drivers/regulator/lp8788-*.c
  F:    include/linux/mfd/lp8788*.h
  
@@@ -12137,15 -11883,6 +12145,15 @@@ W: https://linuxtv.or
  T:    git git://linuxtv.org/media_tree.git
  S:    Odd fixes
  F:    drivers/media/usb/tm6000/
 +F:    Documentation/media/v4l-drivers/tm6000*
 +
 +TW5864 VIDEO4LINUX DRIVER
 +M:    Bluecherry Maintainers <maintainers@bluecherrydvr.com>
 +M:    Andrey Utkin <andrey.utkin@corp.bluecherry.net>
 +M:    Andrey Utkin <andrey_utkin@fastmail.com>
 +L:    linux-media@vger.kernel.org
 +S:    Supported
 +F:    drivers/media/pci/tw5864/
  
  TW68 VIDEO4LINUX DRIVER
  M:    Hans Verkuil <hverkuil@xs4all.nl>
@@@ -12263,6 -12000,12 +12271,6 @@@ S:  Maintaine
  F:    drivers/tc/
  F:    include/linux/tc.h
  
 -U14-34F SCSI DRIVER
 -M:    Dario Ballabio <ballabio_dario@emc.com>
 -L:    linux-scsi@vger.kernel.org
 -S:    Maintained
 -F:    drivers/scsi/u14-34f.c
 -
  UBI FILE SYSTEM (UBIFS)
  M:    Richard Weinberger <richard@nod.at>
  M:    Artem Bityutskiy <dedekind1@gmail.com>
@@@ -12458,7 -12201,7 +12466,7 @@@ S:   Maintaine
  F:    drivers/net/usb/lan78xx.*
  
  USB MASS STORAGE DRIVER
 -M:    Matthew Dharm <mdharm-usb@one-eyed-alien.net>
 +M:    Alan Stern <stern@rowland.harvard.edu>
  L:    linux-usb@vger.kernel.org
  L:    usb-storage@lists.one-eyed-alien.net
  S:    Maintained
@@@ -12542,7 -12285,6 +12550,7 @@@ F:   drivers/net/usb/rtl8150.
  USB SERIAL SUBSYSTEM
  M:    Johan Hovold <johan@kernel.org>
  L:    linux-usb@vger.kernel.org
 +T:    git git://git.kernel.org/pub/scm/linux/kernel/git/johan/usb-serial.git
  S:    Maintained
  F:    Documentation/usb/usb-serial.txt
  F:    drivers/usb/serial/
@@@ -12556,7 -12298,6 +12564,7 @@@ F:   drivers/net/usb/smsc75xx.
  
  USB SMSC95XX ETHERNET DRIVER
  M:    Steve Glendinning <steve.glendinning@shawell.net>
 +M:    Microchip Linux Driver Support <UNGLinuxDriver@microchip.com>
  L:    netdev@vger.kernel.org
  S:    Maintained
  F:    drivers/net/usb/smsc95xx.*
@@@ -12638,7 -12379,7 +12646,7 @@@ L:   linux-media@vger.kernel.or
  T:    git git://linuxtv.org/media_tree.git
  W:    http://royale.zerezo.com/zr364xx/
  S:    Maintained
 -F:    Documentation/video4linux/zr364xx.txt
 +F:    Documentation/media/v4l-drivers/zr364xx*
  F:    drivers/media/usb/zr364xx/
  
  ULPI BUS
@@@ -12663,6 -12404,7 +12671,6 @@@ F:   fs/hostfs
  F:    fs/hppfs/
  
  USERSPACE I/O (UIO)
 -M:    "Hans J. Koch" <hjk@hansjkoch.de>
  M:    Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  S:    Maintained
  T:    git git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/char-misc.git
@@@ -12990,6 -12732,12 +12998,6 @@@ F:  drivers/watchdog
  F:    include/linux/watchdog.h
  F:    include/uapi/linux/watchdog.h
  
 -WD7000 SCSI DRIVER
 -M:    Miroslav Zagorac <zaga@fly.cc.fer.hr>
 -L:    linux-scsi@vger.kernel.org
 -S:    Maintained
 -F:    drivers/scsi/wd7000.c
 -
  WIIMOTE HID DRIVER
  M:    David Herrmann <dh.herrmann@googlemail.com>
  L:    linux-input@vger.kernel.org
@@@ -13059,7 -12807,7 +13067,7 @@@ F:   drivers/input/touchscreen/wm97*.
  F:    drivers/mfd/arizona*
  F:    drivers/mfd/wm*.c
  F:    drivers/mfd/cs47l24*
 -F:    drivers/power/wm83*.c
 +F:    drivers/power/supply/wm83*.c
  F:    drivers/rtc/rtc-wm83*.c
  F:    drivers/regulator/wm8*.c
  F:    drivers/video/backlight/wm83*_bl.c
@@@ -13174,7 -12922,6 +13182,7 @@@ F:   arch/arm64/include/asm/xen
  
  XEN NETWORK BACKEND DRIVER
  M:    Wei Liu <wei.liu2@citrix.com>
 +M:    Paul Durrant <paul.durrant@citrix.com>
  L:    xen-devel@lists.xenproject.org (moderated for non-subscribers)
  L:    netdev@vger.kernel.org
  S:    Supported
@@@ -13212,10 -12959,11 +13220,10 @@@ F:        arch/x86/xen/*swiotlb
  F:    drivers/xen/*swiotlb*
  
  XFS FILESYSTEM
 -P:    Silicon Graphics Inc
  M:    Dave Chinner <david@fromorbit.com>
 -M:    xfs@oss.sgi.com
 -L:    xfs@oss.sgi.com
 -W:    http://oss.sgi.com/projects/xfs
 +M:    linux-xfs@vger.kernel.org
 +L:    linux-xfs@vger.kernel.org
 +W:    http://xfs.org/
  T:    git git://git.kernel.org/pub/scm/linux/kernel/git/dgc/linux-xfs.git
  S:    Supported
  F:    Documentation/filesystems/xfs.txt
@@@ -9,6 -9,7 +9,7 @@@
  
  #include "am33xx.dtsi"
  #include "am335x-bone-common.dtsi"
+ #include <dt-bindings/display/tda998x.h>
  
  / {
        model = "TI AM335x BeagleBone Black";
        status = "okay";
  };
  
 -&cpu0_opp_table {
 -      /*
 -       * All PG 2.0 silicon may not support 1GHz but some of the early
 -       * BeagleBone Blacks have PG 2.0 silicon which is guaranteed
 -       * to support 1GHz OPP so enable it for PG 2.0 on this board.
 -       */
 -      oppnitro@1000000000 {
 -              opp-supported-hw = <0x06 0x0100>;
 -      };
 -};
 -
  &am33xx_pinmux {
        nxp_hdmi_bonelt_pins: nxp_hdmi_bonelt_pins {
                pinctrl-single,pins = <
                        AM33XX_IOPAD(0x9b0, PIN_OUTPUT_PULLDOWN | MUX_MODE3)    /* xdma_event_intr0 */
                >;
        };
+       mcasp0_pins: mcasp0_pins {
+               pinctrl-single,pins = <
+                       AM33XX_IOPAD(0x9ac, PIN_INPUT_PULLUP | MUX_MODE0) /* mcasp0_ahcklx.mcasp0_ahclkx */
+                       AM33XX_IOPAD(0x99c, PIN_OUTPUT_PULLDOWN | MUX_MODE2) /* mcasp0_ahclkr.mcasp0_axr2*/
+                       AM33XX_IOPAD(0x994, PIN_OUTPUT_PULLUP | MUX_MODE0) /* mcasp0_fsx.mcasp0_fsx */
+                       AM33XX_IOPAD(0x990, PIN_OUTPUT_PULLDOWN | MUX_MODE0) /* mcasp0_aclkx.mcasp0_aclkx */
+                       AM33XX_IOPAD(0x86c, PIN_OUTPUT_PULLDOWN | MUX_MODE7) /* gpmc_a11.GPIO1_27 */
+               >;
+       };
  };
  
  &lcdc {
  };
  
  &i2c0 {
-       tda19988 {
+       tda19988: tda19988 {
                compatible = "nxp,tda998x";
                reg = <0x70>;
                pinctrl-names = "default", "off";
                pinctrl-0 = <&nxp_hdmi_bonelt_pins>;
                pinctrl-1 = <&nxp_hdmi_bonelt_off_pins>;
  
-               port {
-                       hdmi_0: endpoint@0 {
-                               remote-endpoint = <&lcdc_0>;
+               #sound-dai-cells = <0>;
+               audio-ports = < TDA998x_I2S     0x03>;
+               ports {
+                       port@0 {
+                               hdmi_0: endpoint@0 {
+                                       remote-endpoint = <&lcdc_0>;
+                               };
                        };
                };
        };
  &rtc {
        system-power-controller;
  };
+ &mcasp0       {
+       #sound-dai-cells = <0>;
+       pinctrl-names = "default";
+       pinctrl-0 = <&mcasp0_pins>;
+       status = "okay";
+       op-mode = <0>;  /* MCASP_IIS_MODE */
+       tdm-slots = <2>;
+       serial-dir = <  /* 0: INACTIVE, 1: TX, 2: RX */
+                       0 0 1 0
+               >;
+       tx-num-evt = <32>;
+       rx-num-evt = <32>;
+ };
+ / {
+       clk_mcasp0_fixed: clk_mcasp0_fixed {
+               #clock-cells = <0>;
+               compatible = "fixed-clock";
+               clock-frequency = <24576000>;
+       };
+       clk_mcasp0: clk_mcasp0 {
+               #clock-cells = <0>;
+               compatible = "gpio-gate-clock";
+               clocks = <&clk_mcasp0_fixed>;
+               enable-gpios = <&gpio1 27 0>; /* BeagleBone Black Clk enable on GPIO1_27 */
+       };
+       sound {
+               compatible = "simple-audio-card";
+               simple-audio-card,name = "TI BeagleBone Black";
+               simple-audio-card,format = "i2s";
+               simple-audio-card,bitclock-master = <&dailink0_master>;
+               simple-audio-card,frame-master = <&dailink0_master>;
+               dailink0_master: simple-audio-card,cpu {
+                       sound-dai = <&mcasp0>;
+                       clocks = <&clk_mcasp0>;
+               };
+               simple-audio-card,codec {
+                       sound-dai = <&tda19988>;
+               };
+       };
+ };
@@@ -317,16 -317,11 +317,11 @@@ static phys_addr_t __init i85x_stolen_b
  static phys_addr_t __init i865_stolen_base(int num, int slot, int func,
                                           size_t stolen_size)
  {
-       u16 toud;
+       u16 toud = 0;
  
-       /*
-        * FIXME is the graphics stolen memory region
-        * always at TOUD? Ie. is it always the last
-        * one to be allocated by the BIOS?
-        */
        toud = read_pci_config_16(0, 0, 0, I865_TOUD);
  
-       return (phys_addr_t)toud << 16;
+       return (phys_addr_t)(toud << 16) + i845_tseg_size();
  }
  
  static phys_addr_t __init gen3_stolen_base(int num, int slot, int func,
@@@ -512,8 -507,7 +507,7 @@@ static const struct pci_device_id intel
        INTEL_I915GM_IDS(&gen3_early_ops),
        INTEL_I945G_IDS(&gen3_early_ops),
        INTEL_I945GM_IDS(&gen3_early_ops),
-       INTEL_VLV_M_IDS(&gen6_early_ops),
-       INTEL_VLV_D_IDS(&gen6_early_ops),
+       INTEL_VLV_IDS(&gen6_early_ops),
        INTEL_PINEVIEW_IDS(&gen3_early_ops),
        INTEL_I965G_IDS(&gen3_early_ops),
        INTEL_G33_IDS(&gen3_early_ops),
        INTEL_SNB_M_IDS(&gen6_early_ops),
        INTEL_IVB_M_IDS(&gen6_early_ops),
        INTEL_IVB_D_IDS(&gen6_early_ops),
-       INTEL_HSW_D_IDS(&gen6_early_ops),
-       INTEL_HSW_M_IDS(&gen6_early_ops),
-       INTEL_BDW_M_IDS(&gen8_early_ops),
-       INTEL_BDW_D_IDS(&gen8_early_ops),
+       INTEL_HSW_IDS(&gen6_early_ops),
+       INTEL_BDW_IDS(&gen8_early_ops),
        INTEL_CHV_IDS(&chv_early_ops),
        INTEL_SKL_IDS(&gen9_early_ops),
        INTEL_BXT_IDS(&gen9_early_ops),
@@@ -555,7 -547,7 +547,7 @@@ intel_graphics_stolen(int num, int slot
  
        /* Mark this space as reserved */
        e820_add_region(base, size, E820_RESERVED);
 -      sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
 +      sanitize_e820_map(e820->map, ARRAY_SIZE(e820->map), &e820->nr_map);
  }
  
  static void __init intel_graphics_quirks(int num, int slot, int func)
index fab9520,0000000..2dd4c3d
mode 100644,000000..100644
--- /dev/null
@@@ -1,230 -1,0 +1,236 @@@
-                  sync_status_str(atomic_read(&sync_file->status)));
 +/*
 + * Sync File validation framework and debug information
 + *
 + * Copyright (C) 2012 Google, Inc.
 + *
 + * This software is licensed under the terms of the GNU General Public
 + * License version 2, as published by the Free Software Foundation, and
 + * may be copied, distributed, and modified under those terms.
 + *
 + * This program is distributed in the hope that it will be useful,
 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 + * GNU General Public License for more details.
 + *
 + */
 +
 +#include <linux/debugfs.h>
 +#include "sync_debug.h"
 +
 +static struct dentry *dbgfs;
 +
 +static LIST_HEAD(sync_timeline_list_head);
 +static DEFINE_SPINLOCK(sync_timeline_list_lock);
 +static LIST_HEAD(sync_file_list_head);
 +static DEFINE_SPINLOCK(sync_file_list_lock);
 +
 +void sync_timeline_debug_add(struct sync_timeline *obj)
 +{
 +      unsigned long flags;
 +
 +      spin_lock_irqsave(&sync_timeline_list_lock, flags);
 +      list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
 +      spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
 +}
 +
 +void sync_timeline_debug_remove(struct sync_timeline *obj)
 +{
 +      unsigned long flags;
 +
 +      spin_lock_irqsave(&sync_timeline_list_lock, flags);
 +      list_del(&obj->sync_timeline_list);
 +      spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
 +}
 +
 +void sync_file_debug_add(struct sync_file *sync_file)
 +{
 +      unsigned long flags;
 +
 +      spin_lock_irqsave(&sync_file_list_lock, flags);
 +      list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
 +      spin_unlock_irqrestore(&sync_file_list_lock, flags);
 +}
 +
 +void sync_file_debug_remove(struct sync_file *sync_file)
 +{
 +      unsigned long flags;
 +
 +      spin_lock_irqsave(&sync_file_list_lock, flags);
 +      list_del(&sync_file->sync_file_list);
 +      spin_unlock_irqrestore(&sync_file_list_lock, flags);
 +}
 +
 +static const char *sync_status_str(int status)
 +{
 +      if (status == 0)
 +              return "signaled";
 +
 +      if (status > 0)
 +              return "active";
 +
 +      return "error";
 +}
 +
 +static void sync_print_fence(struct seq_file *s, struct fence *fence, bool show)
 +{
 +      int status = 1;
 +      struct sync_timeline *parent = fence_parent(fence);
 +
 +      if (fence_is_signaled_locked(fence))
 +              status = fence->status;
 +
 +      seq_printf(s, "  %s%sfence %s",
 +                 show ? parent->name : "",
 +                 show ? "_" : "",
 +                 sync_status_str(status));
 +
 +      if (status <= 0) {
 +              struct timespec64 ts64 =
 +                      ktime_to_timespec64(fence->timestamp);
 +
 +              seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
 +      }
 +
 +      if (fence->ops->timeline_value_str &&
 +              fence->ops->fence_value_str) {
 +              char value[64];
 +              bool success;
 +
 +              fence->ops->fence_value_str(fence, value, sizeof(value));
 +              success = strlen(value);
 +
 +              if (success) {
 +                      seq_printf(s, ": %s", value);
 +
 +                      fence->ops->timeline_value_str(fence, value,
 +                                                     sizeof(value));
 +
 +                      if (strlen(value))
 +                              seq_printf(s, " / %s", value);
 +              }
 +      }
 +
 +      seq_puts(s, "\n");
 +}
 +
 +static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
 +{
 +      struct list_head *pos;
 +      unsigned long flags;
 +
 +      seq_printf(s, "%s: %d\n", obj->name, obj->value);
 +
 +      spin_lock_irqsave(&obj->child_list_lock, flags);
 +      list_for_each(pos, &obj->child_list_head) {
 +              struct sync_pt *pt =
 +                      container_of(pos, struct sync_pt, child_list);
 +              sync_print_fence(s, &pt->base, false);
 +      }
 +      spin_unlock_irqrestore(&obj->child_list_lock, flags);
 +}
 +
 +static void sync_print_sync_file(struct seq_file *s,
 +                                struct sync_file *sync_file)
 +{
 +      int i;
 +
 +      seq_printf(s, "[%p] %s: %s\n", sync_file, sync_file->name,
-       for (i = 0; i < sync_file->num_fences; ++i)
-               sync_print_fence(s, sync_file->cbs[i].fence, true);
++                 sync_status_str(!fence_is_signaled(sync_file->fence)));
 +
++      if (fence_is_array(sync_file->fence)) {
++              struct fence_array *array = to_fence_array(sync_file->fence);
++
++              for (i = 0; i < array->num_fences; ++i)
++                      sync_print_fence(s, array->fences[i], true);
++      } else {
++              sync_print_fence(s, sync_file->fence, true);
++      }
 +}
 +
 +static int sync_debugfs_show(struct seq_file *s, void *unused)
 +{
 +      unsigned long flags;
 +      struct list_head *pos;
 +
 +      seq_puts(s, "objs:\n--------------\n");
 +
 +      spin_lock_irqsave(&sync_timeline_list_lock, flags);
 +      list_for_each(pos, &sync_timeline_list_head) {
 +              struct sync_timeline *obj =
 +                      container_of(pos, struct sync_timeline,
 +                                   sync_timeline_list);
 +
 +              sync_print_obj(s, obj);
 +              seq_puts(s, "\n");
 +      }
 +      spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
 +
 +      seq_puts(s, "fences:\n--------------\n");
 +
 +      spin_lock_irqsave(&sync_file_list_lock, flags);
 +      list_for_each(pos, &sync_file_list_head) {
 +              struct sync_file *sync_file =
 +                      container_of(pos, struct sync_file, sync_file_list);
 +
 +              sync_print_sync_file(s, sync_file);
 +              seq_puts(s, "\n");
 +      }
 +      spin_unlock_irqrestore(&sync_file_list_lock, flags);
 +      return 0;
 +}
 +
 +static int sync_info_debugfs_open(struct inode *inode, struct file *file)
 +{
 +      return single_open(file, sync_debugfs_show, inode->i_private);
 +}
 +
 +static const struct file_operations sync_info_debugfs_fops = {
 +      .open           = sync_info_debugfs_open,
 +      .read           = seq_read,
 +      .llseek         = seq_lseek,
 +      .release        = single_release,
 +};
 +
 +static __init int sync_debugfs_init(void)
 +{
 +      dbgfs = debugfs_create_dir("sync", NULL);
 +
 +      /*
 +       * The debugfs files won't ever get removed and thus, there is
 +       * no need to protect it against removal races. The use of
 +       * debugfs_create_file_unsafe() is actually safe here.
 +       */
 +      debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
 +                                 &sync_info_debugfs_fops);
 +      debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
 +                                 &sw_sync_debugfs_fops);
 +
 +      return 0;
 +}
 +late_initcall(sync_debugfs_init);
 +
 +#define DUMP_CHUNK 256
 +static char sync_dump_buf[64 * 1024];
 +void sync_dump(void)
 +{
 +      struct seq_file s = {
 +              .buf = sync_dump_buf,
 +              .size = sizeof(sync_dump_buf) - 1,
 +      };
 +      int i;
 +
 +      sync_debugfs_show(&s, NULL);
 +
 +      for (i = 0; i < s.count; i += DUMP_CHUNK) {
 +              if ((s.count - i) > DUMP_CHUNK) {
 +                      char c = s.buf[i + DUMP_CHUNK];
 +
 +                      s.buf[i + DUMP_CHUNK] = 0;
 +                      pr_cont("%s", s.buf + i);
 +                      s.buf[i + DUMP_CHUNK] = c;
 +              } else {
 +                      s.buf[s.count] = 0;
 +                      pr_cont("%s", s.buf + i);
 +              }
 +      }
 +}
  #include "atom.h"
  #include "amdgpu_atombios.h"
  #include "amd_pcie.h"
+ #ifdef CONFIG_DRM_AMDGPU_SI
+ #include "si.h"
+ #endif
  #ifdef CONFIG_DRM_AMDGPU_CIK
  #include "cik.h"
  #endif
  #include "vi.h"
  #include "bif/bif_4_1_d.h"
+ #include <linux/pci.h>
+ #include <linux/firmware.h>
  
  static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
  static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
  
  static const char *amdgpu_asic_name[] = {
+       "TAHITI",
+       "PITCAIRN",
+       "VERDE",
+       "OLAND",
+       "HAINAN",
        "BONAIRE",
        "KAVERI",
        "KABINI",
@@@ -101,7 -111,7 +111,7 @@@ void amdgpu_mm_wreg(struct amdgpu_devic
                    bool always_indirect)
  {
        trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
-       
        if ((reg * 4) < adev->rmmio_size && !always_indirect)
                writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
        else {
@@@ -642,6 -652,46 +652,46 @@@ bool amdgpu_card_posted(struct amdgpu_d
  
  }
  
+ static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
+ {
+       if (amdgpu_sriov_vf(adev))
+               return false;
+       if (amdgpu_passthrough(adev)) {
+               /* for FIJI: In whole GPU pass-through virtualization case
+                * old smc fw won't clear some registers (e.g. MEM_SIZE, BIOS_SCRATCH)
+                * so amdgpu_card_posted return false and driver will incorrectly skip vPost.
+                * but if we force vPost do in pass-through case, the driver reload will hang.
+                * whether doing vPost depends on amdgpu_card_posted if smc version is above
+                * 00160e00 for FIJI.
+                */
+               if (adev->asic_type == CHIP_FIJI) {
+                       int err;
+                       uint32_t fw_ver;
+                       err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
+                       /* force vPost if error occured */
+                       if (err)
+                               return true;
+                       fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
+                       if (fw_ver >= 0x00160e00)
+                               return !amdgpu_card_posted(adev);
+               }
+       } else {
+               /* in bare-metal case, amdgpu_card_posted return false
+                * after system reboot/boot, and return true if driver
+                * reloaded.
+                * we shouldn't do vPost after driver reload otherwise GPU
+                * could hang.
+                */
+               if (amdgpu_card_posted(adev))
+                       return false;
+       }
+       /* we assume vPost is neede for all other cases */
+       return true;
+ }
  /**
   * amdgpu_dummy_page_init - init dummy page used by the driver
   *
@@@ -1026,7 -1076,7 +1076,7 @@@ static void amdgpu_switcheroo_set_state
                /* don't suspend or resume card normally */
                dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  
-               amdgpu_resume_kms(dev, true, true);
+               amdgpu_device_resume(dev, true, true);
  
                dev->pdev->d3_delay = d3_delay;
  
                printk(KERN_INFO "amdgpu: switched off\n");
                drm_kms_helper_poll_disable(dev);
                dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
-               amdgpu_suspend_kms(dev, true, true);
+               amdgpu_device_suspend(dev, true, true);
                dev->switch_power_state = DRM_SWITCH_POWER_OFF;
        }
  }
@@@ -1181,10 -1231,38 +1231,38 @@@ int amdgpu_ip_block_version_cmp(struct 
        return 1;
  }
  
+ static void amdgpu_whether_enable_virtual_display(struct amdgpu_device *adev)
+ {
+       adev->enable_virtual_display = false;
+       if (amdgpu_virtual_display) {
+               struct drm_device *ddev = adev->ddev;
+               const char *pci_address_name = pci_name(ddev->pdev);
+               char *pciaddstr, *pciaddstr_tmp, *pciaddname;
+               pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
+               pciaddstr_tmp = pciaddstr;
+               while ((pciaddname = strsep(&pciaddstr_tmp, ";"))) {
+                       if (!strcmp(pci_address_name, pciaddname)) {
+                               adev->enable_virtual_display = true;
+                               break;
+                       }
+               }
+               DRM_INFO("virtual display string:%s, %s:virtual_display:%d\n",
+                                amdgpu_virtual_display, pci_address_name,
+                                adev->enable_virtual_display);
+               kfree(pciaddstr);
+       }
+ }
  static int amdgpu_early_init(struct amdgpu_device *adev)
  {
        int i, r;
  
+       amdgpu_whether_enable_virtual_display(adev);
        switch (adev->asic_type) {
        case CHIP_TOPAZ:
        case CHIP_TONGA:
                if (r)
                        return r;
                break;
+ #ifdef CONFIG_DRM_AMDGPU_SI
+       case CHIP_VERDE:
+       case CHIP_TAHITI:
+       case CHIP_PITCAIRN:
+       case CHIP_OLAND:
+       case CHIP_HAINAN:
+               adev->family = AMDGPU_FAMILY_SI;
+               r = si_set_ip_blocks(adev);
+               if (r)
+                       return r;
+               break;
+ #endif
  #ifdef CONFIG_DRM_AMDGPU_CIK
        case CHIP_BONAIRE:
        case CHIP_HAWAII:
@@@ -1318,6 -1408,9 +1408,9 @@@ static int amdgpu_late_init(struct amdg
        for (i = 0; i < adev->num_ip_blocks; i++) {
                if (!adev->ip_block_status[i].valid)
                        continue;
+               if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_UVD ||
+                       adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_VCE)
+                       continue;
                /* enable clockgating to save power */
                r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
                                                                    AMD_CG_STATE_GATE);
                                DRM_ERROR("late_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
                                return r;
                        }
+                       adev->ip_block_status[i].late_initialized = true;
                }
        }
  
@@@ -1376,8 -1470,11 +1470,11 @@@ static int amdgpu_fini(struct amdgpu_de
        }
  
        for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
+               if (!adev->ip_block_status[i].late_initialized)
+                       continue;
                if (adev->ip_blocks[i].funcs->late_fini)
                        adev->ip_blocks[i].funcs->late_fini((void *)adev);
+               adev->ip_block_status[i].late_initialized = false;
        }
  
        return 0;
@@@ -1433,13 -1530,10 +1530,10 @@@ static int amdgpu_resume(struct amdgpu_
        return 0;
  }
  
- static bool amdgpu_device_is_virtual(void)
+ static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
  {
- #ifdef CONFIG_X86
-       return boot_cpu_has(X86_FEATURE_HYPERVISOR);
- #else
-       return false;
- #endif
+       if (amdgpu_atombios_has_gpu_virtualization_table(adev))
+               adev->virtualization.virtual_caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  }
  
  /**
@@@ -1461,6 -1555,7 +1555,7 @@@ int amdgpu_device_init(struct amdgpu_de
  {
        int r, i;
        bool runtime = false;
+       u32 max_MBps;
  
        adev->shutdown = false;
        adev->dev = &pdev->dev;
        adev->smc_wreg = &amdgpu_invalid_wreg;
        adev->pcie_rreg = &amdgpu_invalid_rreg;
        adev->pcie_wreg = &amdgpu_invalid_wreg;
+       adev->pciep_rreg = &amdgpu_invalid_rreg;
+       adev->pciep_wreg = &amdgpu_invalid_wreg;
        adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
        adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
        adev->didt_rreg = &amdgpu_invalid_rreg;
        spin_lock_init(&adev->didt_idx_lock);
        spin_lock_init(&adev->gc_cac_idx_lock);
        spin_lock_init(&adev->audio_endpt_idx_lock);
+       spin_lock_init(&adev->mm_stats.lock);
+       INIT_LIST_HEAD(&adev->shadow_list);
+       mutex_init(&adev->shadow_list_lock);
+       INIT_LIST_HEAD(&adev->gtt_list);
+       spin_lock_init(&adev->gtt_list_lock);
+       if (adev->asic_type >= CHIP_BONAIRE) {
+               adev->rmmio_base = pci_resource_start(adev->pdev, 5);
+               adev->rmmio_size = pci_resource_len(adev->pdev, 5);
+       } else {
+               adev->rmmio_base = pci_resource_start(adev->pdev, 2);
+               adev->rmmio_size = pci_resource_len(adev->pdev, 2);
+       }
  
-       adev->rmmio_base = pci_resource_start(adev->pdev, 5);
-       adev->rmmio_size = pci_resource_len(adev->pdev, 5);
        adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
        if (adev->rmmio == NULL) {
                return -ENOMEM;
        DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
        DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
  
-       /* doorbell bar mapping */
-       amdgpu_doorbell_init(adev);
+       if (adev->asic_type >= CHIP_BONAIRE)
+               /* doorbell bar mapping */
+               amdgpu_doorbell_init(adev);
  
        /* io port mapping */
        for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
                goto failed;
        }
  
-       /* See if the asic supports SR-IOV */
-       adev->virtualization.supports_sr_iov =
-               amdgpu_atombios_has_gpu_virtualization_table(adev);
-       /* Check if we are executing in a virtualized environment */
-       adev->virtualization.is_virtual = amdgpu_device_is_virtual();
-       adev->virtualization.caps = amdgpu_asic_get_virtual_caps(adev);
+       /* detect if we are with an SRIOV vbios */
+       amdgpu_device_detect_sriov_bios(adev);
  
        /* Post card if necessary */
-       if (!amdgpu_card_posted(adev) ||
-           (adev->virtualization.is_virtual &&
-            !(adev->virtualization.caps & AMDGPU_VIRT_CAPS_SRIOV_EN))) {
+       if (amdgpu_vpost_needed(adev)) {
                if (!adev->bios) {
-                       dev_err(adev->dev, "Card not posted and no BIOS - ignoring\n");
+                       dev_err(adev->dev, "no vBIOS found\n");
                        r = -EINVAL;
                        goto failed;
                }
-               DRM_INFO("GPU not posted. posting now...\n");
-               amdgpu_atom_asic_init(adev->mode_info.atom_context);
+               DRM_INFO("GPU posting now...\n");
+               r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
+               if (r) {
+                       dev_err(adev->dev, "gpu post error!\n");
+                       goto failed;
+               }
+       } else {
+               DRM_INFO("GPU post is not needed\n");
        }
  
        /* Initialize clocks */
  
        adev->accel_working = true;
  
+       /* Initialize the buffer migration limit. */
+       if (amdgpu_moverate >= 0)
+               max_MBps = amdgpu_moverate;
+       else
+               max_MBps = 8; /* Allow 8 MB/s. */
+       /* Get a log2 for easy divisions. */
+       adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
        amdgpu_fbdev_init(adev);
  
        r = amdgpu_ib_pool_init(adev);
@@@ -1708,11 -1826,11 +1826,11 @@@ void amdgpu_device_fini(struct amdgpu_d
  
        DRM_INFO("amdgpu: finishing device.\n");
        adev->shutdown = true;
 +      drm_crtc_force_disable_all(adev->ddev);
        /* evict vram memory */
        amdgpu_bo_evict_vram(adev);
        amdgpu_ib_pool_fini(adev);
        amdgpu_fence_driver_fini(adev);
 -      drm_crtc_force_disable_all(adev->ddev);
        amdgpu_fbdev_fini(adev);
        r = amdgpu_fini(adev);
        kfree(adev->ip_block_status);
        adev->rio_mem = NULL;
        iounmap(adev->rmmio);
        adev->rmmio = NULL;
-       amdgpu_doorbell_fini(adev);
+       if (adev->asic_type >= CHIP_BONAIRE)
+               amdgpu_doorbell_fini(adev);
        amdgpu_debugfs_regs_cleanup(adev);
        amdgpu_debugfs_remove_files(adev);
  }
   * Suspend & resume.
   */
  /**
-  * amdgpu_suspend_kms - initiate device suspend
+  * amdgpu_device_suspend - initiate device suspend
   *
   * @pdev: drm dev pointer
   * @state: suspend state
   * Returns 0 for success or an error on failure.
   * Called at driver suspend.
   */
- int amdgpu_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon)
+ int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
  {
        struct amdgpu_device *adev;
        struct drm_crtc *crtc;
                /* Shut down the device */
                pci_disable_device(dev->pdev);
                pci_set_power_state(dev->pdev, PCI_D3hot);
+       } else {
+               r = amdgpu_asic_reset(adev);
+               if (r)
+                       DRM_ERROR("amdgpu asic reset failed\n");
        }
  
        if (fbcon) {
  }
  
  /**
-  * amdgpu_resume_kms - initiate device resume
+  * amdgpu_device_resume - initiate device resume
   *
   * @pdev: drm dev pointer
   *
   * Returns 0 for success or an error on failure.
   * Called at driver resume.
   */
- int amdgpu_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
+ int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
  {
        struct drm_connector *connector;
        struct amdgpu_device *adev = dev->dev_private;
        if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
                return 0;
  
-       if (fbcon) {
+       if (fbcon)
                console_lock();
-       }
        if (resume) {
                pci_set_power_state(dev->pdev, PCI_D0);
                pci_restore_state(dev->pdev);
-               if (pci_enable_device(dev->pdev)) {
+               r = pci_enable_device(dev->pdev);
+               if (r) {
                        if (fbcon)
                                console_unlock();
-                       return -1;
+                       return r;
                }
        }
  
        /* post card */
-       if (!amdgpu_card_posted(adev))
-               amdgpu_atom_asic_init(adev->mode_info.atom_context);
+       if (!amdgpu_card_posted(adev) || !resume) {
+               r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
+               if (r)
+                       DRM_ERROR("amdgpu asic init failed\n");
+       }
  
        r = amdgpu_resume(adev);
        if (r)
        return 0;
  }
  
+ static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
+ {
+       int i;
+       bool asic_hang = false;
+       for (i = 0; i < adev->num_ip_blocks; i++) {
+               if (!adev->ip_block_status[i].valid)
+                       continue;
+               if (adev->ip_blocks[i].funcs->check_soft_reset)
+                       adev->ip_blocks[i].funcs->check_soft_reset(adev);
+               if (adev->ip_block_status[i].hang) {
+                       DRM_INFO("IP block:%d is hang!\n", i);
+                       asic_hang = true;
+               }
+       }
+       return asic_hang;
+ }
+ static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
+ {
+       int i, r = 0;
+       for (i = 0; i < adev->num_ip_blocks; i++) {
+               if (!adev->ip_block_status[i].valid)
+                       continue;
+               if (adev->ip_block_status[i].hang &&
+                   adev->ip_blocks[i].funcs->pre_soft_reset) {
+                       r = adev->ip_blocks[i].funcs->pre_soft_reset(adev);
+                       if (r)
+                               return r;
+               }
+       }
+       return 0;
+ }
+ static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
+ {
+       if (adev->ip_block_status[AMD_IP_BLOCK_TYPE_GMC].hang ||
+           adev->ip_block_status[AMD_IP_BLOCK_TYPE_SMC].hang ||
+           adev->ip_block_status[AMD_IP_BLOCK_TYPE_ACP].hang ||
+           adev->ip_block_status[AMD_IP_BLOCK_TYPE_DCE].hang) {
+               DRM_INFO("Some block need full reset!\n");
+               return true;
+       }
+       return false;
+ }
+ static int amdgpu_soft_reset(struct amdgpu_device *adev)
+ {
+       int i, r = 0;
+       for (i = 0; i < adev->num_ip_blocks; i++) {
+               if (!adev->ip_block_status[i].valid)
+                       continue;
+               if (adev->ip_block_status[i].hang &&
+                   adev->ip_blocks[i].funcs->soft_reset) {
+                       r = adev->ip_blocks[i].funcs->soft_reset(adev);
+                       if (r)
+                               return r;
+               }
+       }
+       return 0;
+ }
+ static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
+ {
+       int i, r = 0;
+       for (i = 0; i < adev->num_ip_blocks; i++) {
+               if (!adev->ip_block_status[i].valid)
+                       continue;
+               if (adev->ip_block_status[i].hang &&
+                   adev->ip_blocks[i].funcs->post_soft_reset)
+                       r = adev->ip_blocks[i].funcs->post_soft_reset(adev);
+               if (r)
+                       return r;
+       }
+       return 0;
+ }
+ bool amdgpu_need_backup(struct amdgpu_device *adev)
+ {
+       if (adev->flags & AMD_IS_APU)
+               return false;
+       return amdgpu_lockup_timeout > 0 ? true : false;
+ }
+ static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
+                                          struct amdgpu_ring *ring,
+                                          struct amdgpu_bo *bo,
+                                          struct fence **fence)
+ {
+       uint32_t domain;
+       int r;
+        if (!bo->shadow)
+                return 0;
+        r = amdgpu_bo_reserve(bo, false);
+        if (r)
+                return r;
+        domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
+        /* if bo has been evicted, then no need to recover */
+        if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
+                r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
+                                                NULL, fence, true);
+                if (r) {
+                        DRM_ERROR("recover page table failed!\n");
+                        goto err;
+                }
+        }
+ err:
+        amdgpu_bo_unreserve(bo);
+        return r;
+ }
  /**
   * amdgpu_gpu_reset - reset the asic
   *
@@@ -1949,6 -2196,12 +2196,12 @@@ int amdgpu_gpu_reset(struct amdgpu_devi
  {
        int i, r;
        int resched;
+       bool need_full_reset;
+       if (!amdgpu_check_soft_reset(adev)) {
+               DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
+               return 0;
+       }
  
        atomic_inc(&adev->gpu_reset_counter);
  
        /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
        amdgpu_fence_driver_force_completion(adev);
  
-       /* save scratch */
-       amdgpu_atombios_scratch_regs_save(adev);
-       r = amdgpu_suspend(adev);
+       need_full_reset = amdgpu_need_full_reset(adev);
  
- retry:
-       /* Disable fb access */
-       if (adev->mode_info.num_crtc) {
-               struct amdgpu_mode_mc_save save;
-               amdgpu_display_stop_mc_access(adev, &save);
-               amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
+       if (!need_full_reset) {
+               amdgpu_pre_soft_reset(adev);
+               r = amdgpu_soft_reset(adev);
+               amdgpu_post_soft_reset(adev);
+               if (r || amdgpu_check_soft_reset(adev)) {
+                       DRM_INFO("soft reset failed, will fallback to full reset!\n");
+                       need_full_reset = true;
+               }
        }
  
-       r = amdgpu_asic_reset(adev);
-       /* post card */
-       amdgpu_atom_asic_init(adev->mode_info.atom_context);
+       if (need_full_reset) {
+               /* save scratch */
+               amdgpu_atombios_scratch_regs_save(adev);
+               r = amdgpu_suspend(adev);
  
-       if (!r) {
-               dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
-               r = amdgpu_resume(adev);
+ retry:
+               /* Disable fb access */
+               if (adev->mode_info.num_crtc) {
+                       struct amdgpu_mode_mc_save save;
+                       amdgpu_display_stop_mc_access(adev, &save);
+                       amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
+               }
+               r = amdgpu_asic_reset(adev);
+               /* post card */
+               amdgpu_atom_asic_init(adev->mode_info.atom_context);
+               if (!r) {
+                       dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
+                       r = amdgpu_resume(adev);
+               }
+               /* restore scratch */
+               amdgpu_atombios_scratch_regs_restore(adev);
        }
-       /* restore scratch */
-       amdgpu_atombios_scratch_regs_restore(adev);
        if (!r) {
+               amdgpu_irq_gpu_reset_resume_helper(adev);
+               if (need_full_reset && amdgpu_need_backup(adev)) {
+                       r = amdgpu_ttm_recover_gart(adev);
+                       if (r)
+                               DRM_ERROR("gart recovery failed!!!\n");
+               }
                r = amdgpu_ib_ring_tests(adev);
                if (r) {
                        dev_err(adev->dev, "ib ring test failed (%d).\n", r);
                        r = amdgpu_suspend(adev);
+                       need_full_reset = true;
                        goto retry;
                }
+               /**
+                * recovery vm page tables, since we cannot depend on VRAM is
+                * consistent after gpu full reset.
+                */
+               if (need_full_reset && amdgpu_need_backup(adev)) {
+                       struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
+                       struct amdgpu_bo *bo, *tmp;
+                       struct fence *fence = NULL, *next = NULL;
+                       DRM_INFO("recover vram bo from shadow\n");
+                       mutex_lock(&adev->shadow_list_lock);
+                       list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
+                               amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
+                               if (fence) {
+                                       r = fence_wait(fence, false);
+                                       if (r) {
+                                               WARN(r, "recovery from shadow isn't comleted\n");
+                                               break;
+                                       }
+                               }
  
+                               fence_put(fence);
+                               fence = next;
+                       }
+                       mutex_unlock(&adev->shadow_list_lock);
+                       if (fence) {
+                               r = fence_wait(fence, false);
+                               if (r)
+                                       WARN(r, "recovery from shadow isn't comleted\n");
+                       }
+                       fence_put(fence);
+               }
                for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
                        struct amdgpu_ring *ring = adev->rings[i];
                        if (!ring)
                                continue;
                        amd_sched_job_recovery(&ring->sched);
                        kthread_unpark(ring->sched.thread);
                }
                /* bad news, how to tell it to userspace ? */
                dev_info(adev->dev, "GPU reset failed\n");
        }
-       amdgpu_irq_gpu_reset_resume_helper(adev);
  
        return r;
  }
@@@ -2178,22 -2483,26 +2483,26 @@@ static ssize_t amdgpu_debugfs_regs_read
        struct amdgpu_device *adev = f->f_inode->i_private;
        ssize_t result = 0;
        int r;
-       bool use_bank;
+       bool pm_pg_lock, use_bank;
        unsigned instance_bank, sh_bank, se_bank;
  
        if (size & 0x3 || *pos & 0x3)
                return -EINVAL;
  
+       /* are we reading registers for which a PG lock is necessary? */
+       pm_pg_lock = (*pos >> 23) & 1;
        if (*pos & (1ULL << 62)) {
                se_bank = (*pos >> 24) & 0x3FF;
                sh_bank = (*pos >> 34) & 0x3FF;
                instance_bank = (*pos >> 44) & 0x3FF;
                use_bank = 1;
-               *pos &= 0xFFFFFF;
        } else {
                use_bank = 0;
        }
  
+       *pos &= 0x3FFFF;
        if (use_bank) {
                if (sh_bank >= adev->gfx.config.max_sh_per_se ||
                    se_bank >= adev->gfx.config.max_shader_engines)
                                        sh_bank, instance_bank);
        }
  
+       if (pm_pg_lock)
+               mutex_lock(&adev->pm.mutex);
        while (size) {
                uint32_t value;
  
@@@ -2228,6 -2540,9 +2540,9 @@@ end
                mutex_unlock(&adev->grbm_idx_mutex);
        }
  
+       if (pm_pg_lock)
+               mutex_unlock(&adev->pm.mutex);
        return result;
  }
  
@@@ -2385,7 -2700,7 +2700,7 @@@ static ssize_t amdgpu_debugfs_regs_smc_
        while (size) {
                uint32_t value;
  
-               value = RREG32_SMC(*pos >> 2);
+               value = RREG32_SMC(*pos);
                r = put_user(value, (uint32_t *)buf);
                if (r)
                        return r;
@@@ -2416,7 -2731,7 +2731,7 @@@ static ssize_t amdgpu_debugfs_regs_smc_
                if (r)
                        return r;
  
-               WREG32_SMC(*pos >> 2, value);
+               WREG32_SMC(*pos, value);
  
                result += 4;
                buf += 4;
@@@ -2438,12 -2753,12 +2753,12 @@@ static ssize_t amdgpu_debugfs_gca_confi
        if (size & 0x3 || *pos & 0x3)
                return -EINVAL;
  
-       config = kmalloc(256 * sizeof(*config), GFP_KERNEL);
+       config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
        if (!config)
                return -ENOMEM;
  
        /* version, increment each time something is added */
-       config[no_regs++] = 0;
+       config[no_regs++] = 2;
        config[no_regs++] = adev->gfx.config.max_shader_engines;
        config[no_regs++] = adev->gfx.config.max_tile_pipes;
        config[no_regs++] = adev->gfx.config.max_cu_per_sh;
        config[no_regs++] = adev->gfx.config.gb_addr_config;
        config[no_regs++] = adev->gfx.config.num_rbs;
  
+       /* rev==1 */
+       config[no_regs++] = adev->rev_id;
+       config[no_regs++] = adev->pg_flags;
+       config[no_regs++] = adev->cg_flags;
+       /* rev==2 */
+       config[no_regs++] = adev->family;
+       config[no_regs++] = adev->external_rev_id;
        while (size && (*pos < no_regs * 4)) {
                uint32_t value;
  
        return result;
  }
  
+ static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
+                                       size_t size, loff_t *pos)
+ {
+       struct amdgpu_device *adev = f->f_inode->i_private;
+       int idx, r;
+       int32_t value;
+       if (size != 4 || *pos & 0x3)
+               return -EINVAL;
+       /* convert offset to sensor number */
+       idx = *pos >> 2;
+       if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
+               r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &value);
+       else
+               return -EINVAL;
+       if (!r)
+               r = put_user(value, (int32_t *)buf);
+       return !r ? 4 : r;
+ }
  
  static const struct file_operations amdgpu_debugfs_regs_fops = {
        .owner = THIS_MODULE,
@@@ -2520,12 -2867,19 +2867,19 @@@ static const struct file_operations amd
        .llseek = default_llseek
  };
  
+ static const struct file_operations amdgpu_debugfs_sensors_fops = {
+       .owner = THIS_MODULE,
+       .read = amdgpu_debugfs_sensor_read,
+       .llseek = default_llseek
+ };
  static const struct file_operations *debugfs_regs[] = {
        &amdgpu_debugfs_regs_fops,
        &amdgpu_debugfs_regs_didt_fops,
        &amdgpu_debugfs_regs_pcie_fops,
        &amdgpu_debugfs_regs_smc_fops,
        &amdgpu_debugfs_gca_config_fops,
+       &amdgpu_debugfs_sensors_fops,
  };
  
  static const char *debugfs_regs_names[] = {
        "amdgpu_regs_pcie",
        "amdgpu_regs_smc",
        "amdgpu_gca_config",
+       "amdgpu_sensors",
  };
  
  static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
@@@ -387,7 -387,7 +387,7 @@@ int armada_gem_pwrite_ioctl(struct drm_
        if (!access_ok(VERIFY_READ, ptr, args->size))
                return -EFAULT;
  
 -      ret = fault_in_multipages_readable(ptr, args->size);
 +      ret = fault_in_pages_readable(ptr, args->size);
        if (ret)
                return ret;
  
@@@ -547,7 -547,7 +547,7 @@@ armada_gem_prime_export(struct drm_devi
        exp_info.flags = O_RDWR;
        exp_info.priv = obj;
  
-       return dma_buf_export(&exp_info);
+       return drm_gem_dmabuf_export(dev, &exp_info);
  }
  
  struct drm_gem_object *
  #include <linux/errno.h>
  #include <linux/sched.h>
  #include <linux/i2c.h>
+ #include <linux/seq_file.h>
  #include <drm/drm_dp_helper.h>
- #include <drm/drm_dp_aux_dev.h>
  #include <drm/drmP.h>
  
+ #include "drm_crtc_helper_internal.h"
  /**
   * DOC: dp helpers
   *
@@@ -223,7 -225,7 +225,7 @@@ static int drm_dp_dpcd_access(struct dr
                        err = ret;
        }
  
-       DRM_DEBUG_KMS("too many retries, giving up\n");
+       DRM_DEBUG_KMS("Too many retries, giving up. First error: %d\n", err);
        ret = err;
  
  unlock:
@@@ -438,6 -440,179 +440,179 @@@ int drm_dp_link_configure(struct drm_dp
  }
  EXPORT_SYMBOL(drm_dp_link_configure);
  
+ /**
+  * drm_dp_downstream_max_clock() - extract branch device max
+  *                                 pixel rate for legacy VGA
+  *                                 converter or max TMDS clock
+  *                                 rate for others
+  * @dpcd: DisplayPort configuration data
+  * @port_cap: port capabilities
+  *
+  * Returns max clock in kHz on success or 0 if max clock not defined
+  */
+ int drm_dp_downstream_max_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
+                               const u8 port_cap[4])
+ {
+       int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
+       bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
+               DP_DETAILED_CAP_INFO_AVAILABLE;
+       if (!detailed_cap_info)
+               return 0;
+       switch (type) {
+       case DP_DS_PORT_TYPE_VGA:
+               return port_cap[1] * 8 * 1000;
+       case DP_DS_PORT_TYPE_DVI:
+       case DP_DS_PORT_TYPE_HDMI:
+       case DP_DS_PORT_TYPE_DP_DUALMODE:
+               return port_cap[1] * 2500;
+       default:
+               return 0;
+       }
+ }
+ EXPORT_SYMBOL(drm_dp_downstream_max_clock);
+ /**
+  * drm_dp_downstream_max_bpc() - extract branch device max
+  *                               bits per component
+  * @dpcd: DisplayPort configuration data
+  * @port_cap: port capabilities
+  *
+  * Returns max bpc on success or 0 if max bpc not defined
+  */
+ int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
+                             const u8 port_cap[4])
+ {
+       int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
+       bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
+               DP_DETAILED_CAP_INFO_AVAILABLE;
+       int bpc;
+       if (!detailed_cap_info)
+               return 0;
+       switch (type) {
+       case DP_DS_PORT_TYPE_VGA:
+       case DP_DS_PORT_TYPE_DVI:
+       case DP_DS_PORT_TYPE_HDMI:
+       case DP_DS_PORT_TYPE_DP_DUALMODE:
+               bpc = port_cap[2] & DP_DS_MAX_BPC_MASK;
+               switch (bpc) {
+               case DP_DS_8BPC:
+                       return 8;
+               case DP_DS_10BPC:
+                       return 10;
+               case DP_DS_12BPC:
+                       return 12;
+               case DP_DS_16BPC:
+                       return 16;
+               }
+       default:
+               return 0;
+       }
+ }
+ EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
+ /**
+  * drm_dp_downstream_id() - identify branch device
+  * @aux: DisplayPort AUX channel
+  * @id: DisplayPort branch device id
+  *
+  * Returns branch device id on success or NULL on failure
+  */
+ int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
+ {
+       return drm_dp_dpcd_read(aux, DP_BRANCH_ID, id, 6);
+ }
+ EXPORT_SYMBOL(drm_dp_downstream_id);
+ /**
+  * drm_dp_downstream_debug() - debug DP branch devices
+  * @m: pointer for debugfs file
+  * @dpcd: DisplayPort configuration data
+  * @port_cap: port capabilities
+  * @aux: DisplayPort AUX channel
+  *
+  */
+ void drm_dp_downstream_debug(struct seq_file *m,
+                            const u8 dpcd[DP_RECEIVER_CAP_SIZE],
+                            const u8 port_cap[4], struct drm_dp_aux *aux)
+ {
+       bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
+                                DP_DETAILED_CAP_INFO_AVAILABLE;
+       int clk;
+       int bpc;
+       char id[6];
+       int len;
+       uint8_t rev[2];
+       int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
+       bool branch_device = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
+                            DP_DWN_STRM_PORT_PRESENT;
+       seq_printf(m, "\tDP branch device present: %s\n",
+                  branch_device ? "yes" : "no");
+       if (!branch_device)
+               return;
+       switch (type) {
+       case DP_DS_PORT_TYPE_DP:
+               seq_puts(m, "\t\tType: DisplayPort\n");
+               break;
+       case DP_DS_PORT_TYPE_VGA:
+               seq_puts(m, "\t\tType: VGA\n");
+               break;
+       case DP_DS_PORT_TYPE_DVI:
+               seq_puts(m, "\t\tType: DVI\n");
+               break;
+       case DP_DS_PORT_TYPE_HDMI:
+               seq_puts(m, "\t\tType: HDMI\n");
+               break;
+       case DP_DS_PORT_TYPE_NON_EDID:
+               seq_puts(m, "\t\tType: others without EDID support\n");
+               break;
+       case DP_DS_PORT_TYPE_DP_DUALMODE:
+               seq_puts(m, "\t\tType: DP++\n");
+               break;
+       case DP_DS_PORT_TYPE_WIRELESS:
+               seq_puts(m, "\t\tType: Wireless\n");
+               break;
+       default:
+               seq_puts(m, "\t\tType: N/A\n");
+       }
+       drm_dp_downstream_id(aux, id);
+       seq_printf(m, "\t\tID: %s\n", id);
+       len = drm_dp_dpcd_read(aux, DP_BRANCH_HW_REV, &rev[0], 1);
+       if (len > 0)
+               seq_printf(m, "\t\tHW: %d.%d\n",
+                          (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
+       len = drm_dp_dpcd_read(aux, DP_BRANCH_SW_REV, &rev, 2);
+       if (len > 0)
+               seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
+       if (detailed_cap_info) {
+               clk = drm_dp_downstream_max_clock(dpcd, port_cap);
+               if (clk > 0) {
+                       if (type == DP_DS_PORT_TYPE_VGA)
+                               seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
+                       else
+                               seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
+               }
+               bpc = drm_dp_downstream_max_bpc(dpcd, port_cap);
+               if (bpc > 0)
+                       seq_printf(m, "\t\tMax bpc: %d\n", bpc);
+       }
+ }
+ EXPORT_SYMBOL(drm_dp_downstream_debug);
  /*
   * I2C-over-AUX implementation
   */
@@@ -574,7 -749,17 +749,17 @@@ static int drm_dp_i2c_do_msg(struct drm
                        if (ret == -EBUSY)
                                continue;
  
-                       DRM_DEBUG_KMS("transaction failed: %d\n", ret);
+                       /*
+                        * While timeouts can be errors, they're usually normal
+                        * behavior (for instance, when a driver tries to
+                        * communicate with a non-existant DisplayPort device).
+                        * Avoid spamming the kernel log with timeout errors.
+                        */
+                       if (ret == -ETIMEDOUT)
+                               DRM_DEBUG_KMS_RATELIMITED("transaction timed out\n");
+                       else
+                               DRM_DEBUG_KMS("transaction failed: %d\n", ret);
                        return ret;
                }
  
@@@ -790,12 -975,6 +975,12 @@@ static void unlock_bus(struct i2c_adapt
        mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
  }
  
 +static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
 +      .lock_bus = lock_bus,
 +      .trylock_bus = trylock_bus,
 +      .unlock_bus = unlock_bus,
 +};
 +
  /**
   * drm_dp_aux_init() - minimally initialise an aux channel
   * @aux: DisplayPort AUX channel
@@@ -813,7 -992,9 +998,7 @@@ void drm_dp_aux_init(struct drm_dp_aux 
        aux->ddc.algo_data = aux;
        aux->ddc.retries = 3;
  
 -      aux->ddc.lock_bus = lock_bus;
 -      aux->ddc.trylock_bus = trylock_bus;
 -      aux->ddc.unlock_bus = unlock_bus;
 +      aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
  }
  EXPORT_SYMBOL(drm_dp_aux_init);
  
  #include <drm/drm_vma_manager.h>
  #include <drm/i915_drm.h>
  #include "i915_drv.h"
+ #include "i915_gem_dmabuf.h"
  #include "i915_vgpu.h"
  #include "i915_trace.h"
  #include "intel_drv.h"
+ #include "intel_frontbuffer.h"
  #include "intel_mocs.h"
+ #include <linux/reservation.h>
  #include <linux/shmem_fs.h>
  #include <linux/slab.h>
  #include <linux/swap.h>
  
  static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
- static void
- i915_gem_object_retire__write(struct drm_i915_gem_object *obj);
- static void
- i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring);
  
  static bool cpu_cache_is_coherent(struct drm_device *dev,
                                  enum i915_cache_level level)
@@@ -139,7 -138,6 +138,6 @@@ int i915_mutex_lock_interruptible(struc
        if (ret)
                return ret;
  
-       WARN_ON(i915_verify_lists(dev));
        return 0;
  }
  
@@@ -156,10 -154,10 +154,10 @@@ i915_gem_get_aperture_ioctl(struct drm_
        pinned = 0;
        mutex_lock(&dev->struct_mutex);
        list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
-               if (vma->pin_count)
+               if (i915_vma_is_pinned(vma))
                        pinned += vma->node.size;
        list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
-               if (vma->pin_count)
+               if (i915_vma_is_pinned(vma))
                        pinned += vma->node.size;
        mutex_unlock(&dev->struct_mutex);
  
@@@ -281,23 -279,129 +279,129 @@@ static const struct drm_i915_gem_object
        .release = i915_gem_object_release_phys,
  };
  
- static int
- drop_pages(struct drm_i915_gem_object *obj)
+ int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  {
-       struct i915_vma *vma, *next;
+       struct i915_vma *vma;
+       LIST_HEAD(still_in_list);
        int ret;
  
-       drm_gem_object_reference(&obj->base);
-       list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link)
-               if (i915_vma_unbind(vma))
-                       break;
+       lockdep_assert_held(&obj->base.dev->struct_mutex);
  
-       ret = i915_gem_object_put_pages(obj);
-       drm_gem_object_unreference(&obj->base);
+       /* Closed vma are removed from the obj->vma_list - but they may
+        * still have an active binding on the object. To remove those we
+        * must wait for all rendering to complete to the object (as unbinding
+        * must anyway), and retire the requests.
+        */
+       ret = i915_gem_object_wait_rendering(obj, false);
+       if (ret)
+               return ret;
+       i915_gem_retire_requests(to_i915(obj->base.dev));
+       while ((vma = list_first_entry_or_null(&obj->vma_list,
+                                              struct i915_vma,
+                                              obj_link))) {
+               list_move_tail(&vma->obj_link, &still_in_list);
+               ret = i915_vma_unbind(vma);
+               if (ret)
+                       break;
+       }
+       list_splice(&still_in_list, &obj->vma_list);
  
        return ret;
  }
  
+ /**
+  * Ensures that all rendering to the object has completed and the object is
+  * safe to unbind from the GTT or access from the CPU.
+  * @obj: i915 gem object
+  * @readonly: waiting for just read access or read-write access
+  */
+ int
+ i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
+                              bool readonly)
+ {
+       struct reservation_object *resv;
+       struct i915_gem_active *active;
+       unsigned long active_mask;
+       int idx;
+       lockdep_assert_held(&obj->base.dev->struct_mutex);
+       if (!readonly) {
+               active = obj->last_read;
+               active_mask = i915_gem_object_get_active(obj);
+       } else {
+               active_mask = 1;
+               active = &obj->last_write;
+       }
+       for_each_active(active_mask, idx) {
+               int ret;
+               ret = i915_gem_active_wait(&active[idx],
+                                          &obj->base.dev->struct_mutex);
+               if (ret)
+                       return ret;
+       }
+       resv = i915_gem_object_get_dmabuf_resv(obj);
+       if (resv) {
+               long err;
+               err = reservation_object_wait_timeout_rcu(resv, !readonly, true,
+                                                         MAX_SCHEDULE_TIMEOUT);
+               if (err < 0)
+                       return err;
+       }
+       return 0;
+ }
+ /* A nonblocking variant of the above wait. Must be called prior to
+  * acquiring the mutex for the object, as the object state may change
+  * during this call. A reference must be held by the caller for the object.
+  */
+ static __must_check int
+ __unsafe_wait_rendering(struct drm_i915_gem_object *obj,
+                       struct intel_rps_client *rps,
+                       bool readonly)
+ {
+       struct i915_gem_active *active;
+       unsigned long active_mask;
+       int idx;
+       active_mask = __I915_BO_ACTIVE(obj);
+       if (!active_mask)
+               return 0;
+       if (!readonly) {
+               active = obj->last_read;
+       } else {
+               active_mask = 1;
+               active = &obj->last_write;
+       }
+       for_each_active(active_mask, idx) {
+               int ret;
+               ret = i915_gem_active_wait_unlocked(&active[idx],
+                                                   I915_WAIT_INTERRUPTIBLE,
+                                                   NULL, rps);
+               if (ret)
+                       return ret;
+       }
+       return 0;
+ }
+ static struct intel_rps_client *to_rps_client(struct drm_file *file)
+ {
+       struct drm_i915_file_private *fpriv = file->driver_priv;
+       return &fpriv->rps;
+ }
  int
  i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
                            int align)
        if (obj->base.filp == NULL)
                return -EINVAL;
  
-       ret = drop_pages(obj);
+       ret = i915_gem_object_unbind(obj);
+       if (ret)
+               return ret;
+       ret = i915_gem_object_put_pages(obj);
        if (ret)
                return ret;
  
@@@ -408,7 -516,7 +516,7 @@@ i915_gem_create(struct drm_file *file
  
        ret = drm_gem_handle_create(file, &obj->base, &handle);
        /* drop reference from allocate - handle holds it now */
-       drm_gem_object_unreference_unlocked(&obj->base);
+       i915_gem_object_put_unlocked(obj);
        if (ret)
                return ret;
  
@@@ -502,33 -610,106 +610,106 @@@ __copy_from_user_swizzled(char *gpu_vad
   * flush the object from the CPU cache.
   */
  int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
-                                   int *needs_clflush)
+                                   unsigned int *needs_clflush)
  {
        int ret;
  
        *needs_clflush = 0;
  
-       if (WARN_ON(!i915_gem_object_has_struct_page(obj)))
-               return -EINVAL;
+       if (!i915_gem_object_has_struct_page(obj))
+               return -ENODEV;
+       ret = i915_gem_object_wait_rendering(obj, true);
+       if (ret)
+               return ret;
+       ret = i915_gem_object_get_pages(obj);
+       if (ret)
+               return ret;
+       i915_gem_object_pin_pages(obj);
+       i915_gem_object_flush_gtt_write_domain(obj);
  
-       if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
-               /* If we're not in the cpu read domain, set ourself into the gtt
-                * read domain and manually flush cachelines (if required). This
-                * optimizes for the case when the gpu will dirty the data
-                * anyway again before the next pread happens. */
+       /* If we're not in the cpu read domain, set ourself into the gtt
+        * read domain and manually flush cachelines (if required). This
+        * optimizes for the case when the gpu will dirty the data
+        * anyway again before the next pread happens.
+        */
+       if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
                *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
                                                        obj->cache_level);
-               ret = i915_gem_object_wait_rendering(obj, true);
+       if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
+               ret = i915_gem_object_set_to_cpu_domain(obj, false);
                if (ret)
-                       return ret;
+                       goto err_unpin;
+               *needs_clflush = 0;
        }
  
+       /* return with the pages pinned */
+       return 0;
+ err_unpin:
+       i915_gem_object_unpin_pages(obj);
+       return ret;
+ }
+ int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
+                                    unsigned int *needs_clflush)
+ {
+       int ret;
+       *needs_clflush = 0;
+       if (!i915_gem_object_has_struct_page(obj))
+               return -ENODEV;
+       ret = i915_gem_object_wait_rendering(obj, false);
+       if (ret)
+               return ret;
        ret = i915_gem_object_get_pages(obj);
        if (ret)
                return ret;
  
        i915_gem_object_pin_pages(obj);
  
+       i915_gem_object_flush_gtt_write_domain(obj);
+       /* If we're not in the cpu write domain, set ourself into the
+        * gtt write domain and manually flush cachelines (as required).
+        * This optimizes for the case when the gpu will use the data
+        * right away and we therefore have to clflush anyway.
+        */
+       if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
+               *needs_clflush |= cpu_write_needs_clflush(obj) << 1;
+       /* Same trick applies to invalidate partially written cachelines read
+        * before writing.
+        */
+       if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
+               *needs_clflush |= !cpu_cache_is_coherent(obj->base.dev,
+                                                        obj->cache_level);
+       if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
+               ret = i915_gem_object_set_to_cpu_domain(obj, true);
+               if (ret)
+                       goto err_unpin;
+               *needs_clflush = 0;
+       }
+       if ((*needs_clflush & CLFLUSH_AFTER) == 0)
+               obj->cache_dirty = true;
+       intel_fb_obj_invalidate(obj, ORIGIN_CPU);
+       obj->dirty = 1;
+       /* return with the pages pinned */
+       return 0;
+ err_unpin:
+       i915_gem_object_unpin_pages(obj);
        return ret;
  }
  
@@@ -638,14 -819,24 +819,24 @@@ i915_gem_gtt_pread(struct drm_device *d
  {
        struct drm_i915_private *dev_priv = to_i915(dev);
        struct i915_ggtt *ggtt = &dev_priv->ggtt;
+       struct i915_vma *vma;
        struct drm_mm_node node;
        char __user *user_data;
        uint64_t remain;
        uint64_t offset;
        int ret;
  
-       ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE);
-       if (ret) {
+       vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
+       if (!IS_ERR(vma)) {
+               node.start = i915_ggtt_offset(vma);
+               node.allocated = false;
+               ret = i915_vma_put_fence(vma);
+               if (ret) {
+                       i915_vma_unpin(vma);
+                       vma = ERR_PTR(ret);
+               }
+       }
+       if (IS_ERR(vma)) {
                ret = insert_mappable_node(dev_priv, &node, PAGE_SIZE);
                if (ret)
                        goto out;
                }
  
                i915_gem_object_pin_pages(obj);
-       } else {
-               node.start = i915_gem_obj_ggtt_offset(obj);
-               node.allocated = false;
-               ret = i915_gem_object_put_fence(obj);
-               if (ret)
-                       goto out_unpin;
        }
  
        ret = i915_gem_object_set_to_gtt_domain(obj, false);
  
        mutex_unlock(&dev->struct_mutex);
        if (likely(!i915.prefault_disable)) {
 -              ret = fault_in_multipages_writeable(user_data, remain);
 +              ret = fault_in_pages_writeable(user_data, remain);
                if (ret) {
                        mutex_lock(&dev->struct_mutex);
                        goto out_unpin;
                 * and write to user memory which may result into page
                 * faults, and so we cannot perform this under struct_mutex.
                 */
-               if (slow_user_access(ggtt->mappable, page_base,
+               if (slow_user_access(&ggtt->mappable, page_base,
                                     page_offset, user_data,
                                     page_length, false)) {
                        ret = -EFAULT;
@@@ -739,7 -924,7 +924,7 @@@ out_unpin
                i915_gem_object_unpin_pages(obj);
                remove_mappable_node(&node);
        } else {
-               i915_gem_object_ggtt_unpin(obj);
+               i915_vma_unpin(vma);
        }
  out:
        return ret;
@@@ -760,19 -945,14 +945,14 @@@ i915_gem_shmem_pread(struct drm_device 
        int needs_clflush = 0;
        struct sg_page_iter sg_iter;
  
-       if (!i915_gem_object_has_struct_page(obj))
-               return -ENODEV;
-       user_data = u64_to_user_ptr(args->data_ptr);
-       remain = args->size;
-       obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
        ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
        if (ret)
                return ret;
  
+       obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
+       user_data = u64_to_user_ptr(args->data_ptr);
        offset = args->offset;
+       remain = args->size;
  
        for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
                         offset >> PAGE_SHIFT) {
                mutex_unlock(&dev->struct_mutex);
  
                if (likely(!i915.prefault_disable) && !prefaulted) {
 -                      ret = fault_in_multipages_writeable(user_data, remain);
 +                      ret = fault_in_pages_writeable(user_data, remain);
                        /* Userspace is tricking us, but we've already clobbered
                         * its pages with the prefault and promised to write the
                         * data up to the first fault. Hence ignore any errors
@@@ -828,7 -1008,7 +1008,7 @@@ next_page
        }
  
  out:
-       i915_gem_object_unpin_pages(obj);
+       i915_gem_obj_finish_shmem_access(obj);
  
        return ret;
  }
@@@ -857,25 -1037,27 +1037,27 @@@ i915_gem_pread_ioctl(struct drm_device 
                       args->size))
                return -EFAULT;
  
-       ret = i915_mutex_lock_interruptible(dev);
-       if (ret)
-               return ret;
-       obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
-       if (&obj->base == NULL) {
-               ret = -ENOENT;
-               goto unlock;
-       }
+       obj = i915_gem_object_lookup(file, args->handle);
+       if (!obj)
+               return -ENOENT;
  
        /* Bounds check source.  */
        if (args->offset > obj->base.size ||
            args->size > obj->base.size - args->offset) {
                ret = -EINVAL;
-               goto out;
+               goto err;
        }
  
        trace_i915_gem_object_pread(obj, args->offset, args->size);
  
+       ret = __unsafe_wait_rendering(obj, to_rps_client(file), true);
+       if (ret)
+               goto err;
+       ret = i915_mutex_lock_interruptible(dev);
+       if (ret)
+               goto err;
        ret = i915_gem_shmem_pread(dev, obj, args, file);
  
        /* pread for non shmem backed objects */
                intel_runtime_pm_put(to_i915(dev));
        }
  
- out:
-       drm_gem_object_unreference(&obj->base);
- unlock:
+       i915_gem_object_put(obj);
        mutex_unlock(&dev->struct_mutex);
+       return ret;
+ err:
+       i915_gem_object_put_unlocked(obj);
        return ret;
  }
  
@@@ -919,7 -1104,7 +1104,7 @@@ fast_user_write(struct io_mapping *mapp
  /**
   * This is the fast pwrite path, where we copy the data directly from the
   * user into the GTT, uncached.
-  * @dev: drm device pointer
+  * @i915: i915 device private data
   * @obj: i915 gem object
   * @args: pwrite arguments structure
   * @file: drm file pointer
@@@ -932,17 -1117,28 +1117,28 @@@ i915_gem_gtt_pwrite_fast(struct drm_i91
  {
        struct i915_ggtt *ggtt = &i915->ggtt;
        struct drm_device *dev = obj->base.dev;
+       struct i915_vma *vma;
        struct drm_mm_node node;
        uint64_t remain, offset;
        char __user *user_data;
        int ret;
        bool hit_slow_path = false;
  
-       if (obj->tiling_mode != I915_TILING_NONE)
+       if (i915_gem_object_is_tiled(obj))
                return -EFAULT;
  
-       ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
-       if (ret) {
+       vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
+                                      PIN_MAPPABLE | PIN_NONBLOCK);
+       if (!IS_ERR(vma)) {
+               node.start = i915_ggtt_offset(vma);
+               node.allocated = false;
+               ret = i915_vma_put_fence(vma);
+               if (ret) {
+                       i915_vma_unpin(vma);
+                       vma = ERR_PTR(ret);
+               }
+       }
+       if (IS_ERR(vma)) {
                ret = insert_mappable_node(i915, &node, PAGE_SIZE);
                if (ret)
                        goto out;
                }
  
                i915_gem_object_pin_pages(obj);
-       } else {
-               node.start = i915_gem_obj_ggtt_offset(obj);
-               node.allocated = false;
-               ret = i915_gem_object_put_fence(obj);
-               if (ret)
-                       goto out_unpin;
        }
  
        ret = i915_gem_object_set_to_gtt_domain(obj, true);
        if (ret)
                goto out_unpin;
  
-       intel_fb_obj_invalidate(obj, ORIGIN_GTT);
+       intel_fb_obj_invalidate(obj, ORIGIN_CPU);
        obj->dirty = true;
  
        user_data = u64_to_user_ptr(args->data_ptr);
                 * If the object is non-shmem backed, we retry again with the
                 * path that handles page fault.
                 */
-               if (fast_user_write(ggtt->mappable, page_base,
+               if (fast_user_write(&ggtt->mappable, page_base,
                                    page_offset, user_data, page_length)) {
                        hit_slow_path = true;
                        mutex_unlock(&dev->struct_mutex);
-                       if (slow_user_access(ggtt->mappable,
+                       if (slow_user_access(&ggtt->mappable,
                                             page_base,
                                             page_offset, user_data,
                                             page_length, true)) {
@@@ -1033,7 -1223,7 +1223,7 @@@ out_flush
                }
        }
  
-       intel_fb_obj_flush(obj, false, ORIGIN_GTT);
+       intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  out_unpin:
        if (node.allocated) {
                wmb();
                i915_gem_object_unpin_pages(obj);
                remove_mappable_node(&node);
        } else {
-               i915_gem_object_ggtt_unpin(obj);
+               i915_vma_unpin(vma);
        }
  out:
        return ret;
@@@ -1126,41 -1316,17 +1316,17 @@@ i915_gem_shmem_pwrite(struct drm_devic
        int shmem_page_offset, page_length, ret = 0;
        int obj_do_bit17_swizzling, page_do_bit17_swizzling;
        int hit_slowpath = 0;
-       int needs_clflush_after = 0;
-       int needs_clflush_before = 0;
+       unsigned int needs_clflush;
        struct sg_page_iter sg_iter;
  
-       user_data = u64_to_user_ptr(args->data_ptr);
-       remain = args->size;
-       obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
-       if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
-               /* If we're not in the cpu write domain, set ourself into the gtt
-                * write domain and manually flush cachelines (if required). This
-                * optimizes for the case when the gpu will use the data
-                * right away and we therefore have to clflush anyway. */
-               needs_clflush_after = cpu_write_needs_clflush(obj);
-               ret = i915_gem_object_wait_rendering(obj, false);
-               if (ret)
-                       return ret;
-       }
-       /* Same trick applies to invalidate partially written cachelines read
-        * before writing. */
-       if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
-               needs_clflush_before =
-                       !cpu_cache_is_coherent(dev, obj->cache_level);
-       ret = i915_gem_object_get_pages(obj);
+       ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
        if (ret)
                return ret;
  
-       intel_fb_obj_invalidate(obj, ORIGIN_CPU);
-       i915_gem_object_pin_pages(obj);
+       obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
+       user_data = u64_to_user_ptr(args->data_ptr);
        offset = args->offset;
-       obj->dirty = 1;
+       remain = args->size;
  
        for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
                         offset >> PAGE_SHIFT) {
                /* If we don't overwrite a cacheline completely we need to be
                 * careful to have up-to-date data by first clflushing. Don't
                 * overcomplicate things and flush the entire patch. */
-               partial_cacheline_write = needs_clflush_before &&
+               partial_cacheline_write = needs_clflush & CLFLUSH_BEFORE &&
                        ((shmem_page_offset | page_length)
                                & (boot_cpu_data.x86_clflush_size - 1));
  
                ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
                                        user_data, page_do_bit17_swizzling,
                                        partial_cacheline_write,
-                                       needs_clflush_after);
+                                       needs_clflush & CLFLUSH_AFTER);
                if (ret == 0)
                        goto next_page;
  
                ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
                                        user_data, page_do_bit17_swizzling,
                                        partial_cacheline_write,
-                                       needs_clflush_after);
+                                       needs_clflush & CLFLUSH_AFTER);
  
                mutex_lock(&dev->struct_mutex);
  
@@@ -1217,7 -1383,7 +1383,7 @@@ next_page
        }
  
  out:
-       i915_gem_object_unpin_pages(obj);
+       i915_gem_obj_finish_shmem_access(obj);
  
        if (hit_slowpath) {
                /*
                 * cachelines in-line while writing and the object moved
                 * out of the cpu write domain while we've dropped the lock.
                 */
-               if (!needs_clflush_after &&
+               if (!(needs_clflush & CLFLUSH_AFTER) &&
                    obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
                        if (i915_gem_clflush_object(obj, obj->pin_display))
-                               needs_clflush_after = true;
+                               needs_clflush |= CLFLUSH_AFTER;
                }
        }
  
-       if (needs_clflush_after)
+       if (needs_clflush & CLFLUSH_AFTER)
                i915_gem_chipset_flush(to_i915(dev));
-       else
-               obj->cache_dirty = true;
  
        intel_fb_obj_flush(obj, false, ORIGIN_CPU);
        return ret;
@@@ -1267,33 -1431,35 +1431,35 @@@ i915_gem_pwrite_ioctl(struct drm_devic
                return -EFAULT;
  
        if (likely(!i915.prefault_disable)) {
 -              ret = fault_in_multipages_readable(u64_to_user_ptr(args->data_ptr),
 +              ret = fault_in_pages_readable(u64_to_user_ptr(args->data_ptr),
                                                   args->size);
                if (ret)
                        return -EFAULT;
        }
  
-       intel_runtime_pm_get(dev_priv);
-       ret = i915_mutex_lock_interruptible(dev);
-       if (ret)
-               goto put_rpm;
-       obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
-       if (&obj->base == NULL) {
-               ret = -ENOENT;
-               goto unlock;
-       }
+       obj = i915_gem_object_lookup(file, args->handle);
+       if (!obj)
+               return -ENOENT;
  
        /* Bounds check destination. */
        if (args->offset > obj->base.size ||
            args->size > obj->base.size - args->offset) {
                ret = -EINVAL;
-               goto out;
+               goto err;
        }
  
        trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  
+       ret = __unsafe_wait_rendering(obj, to_rps_client(file), false);
+       if (ret)
+               goto err;
+       intel_runtime_pm_get(dev_priv);
+       ret = i915_mutex_lock_interruptible(dev);
+       if (ret)
+               goto err_rpm;
        ret = -EFAULT;
        /* We can only do the GTT pwrite on untiled buffers, as otherwise
         * it would end up going through the fenced access, and we'll get
        if (ret == -EFAULT || ret == -ENOSPC) {
                if (obj->phys_handle)
                        ret = i915_gem_phys_pwrite(obj, args, file);
-               else if (i915_gem_object_has_struct_page(obj))
-                       ret = i915_gem_shmem_pwrite(dev, obj, args, file);
                else
-                       ret = -ENODEV;
+                       ret = i915_gem_shmem_pwrite(dev, obj, args, file);
        }
  
- out:
-       drm_gem_object_unreference(&obj->base);
- unlock:
+       i915_gem_object_put(obj);
        mutex_unlock(&dev->struct_mutex);
- put_rpm:
        intel_runtime_pm_put(dev_priv);
  
        return ret;
- }
- static int
- i915_gem_check_wedge(unsigned reset_counter, bool interruptible)
- {
-       if (__i915_terminally_wedged(reset_counter))
-               return -EIO;
-       if (__i915_reset_in_progress(reset_counter)) {
-               /* Non-interruptible callers can't handle -EAGAIN, hence return
-                * -EIO unconditionally for these. */
-               if (!interruptible)
-                       return -EIO;
-               return -EAGAIN;
-       }
  
-       return 0;
+ err_rpm:
+       intel_runtime_pm_put(dev_priv);
+ err:
+       i915_gem_object_put_unlocked(obj);
+       return ret;
  }
  
- static unsigned long local_clock_us(unsigned *cpu)
+ static inline enum fb_op_origin
+ write_origin(struct drm_i915_gem_object *obj, unsigned domain)
  {
-       unsigned long t;
-       /* Cheaply and approximately convert from nanoseconds to microseconds.
-        * The result and subsequent calculations are also defined in the same
-        * approximate microseconds units. The principal source of timing
-        * error here is from the simple truncation.
-        *
-        * Note that local_clock() is only defined wrt to the current CPU;
-        * the comparisons are no longer valid if we switch CPUs. Instead of
-        * blocking preemption for the entire busywait, we can detect the CPU
-        * switch and use that as indicator of system load and a reason to
-        * stop busywaiting, see busywait_stop().
-        */
-       *cpu = get_cpu();
-       t = local_clock() >> 10;
-       put_cpu();
-       return t;
- }
- static bool busywait_stop(unsigned long timeout, unsigned cpu)
- {
-       unsigned this_cpu;
-       if (time_after(local_clock_us(&this_cpu), timeout))
-               return true;
-       return this_cpu != cpu;
- }
- bool __i915_spin_request(const struct drm_i915_gem_request *req,
-                        int state, unsigned long timeout_us)
- {
-       unsigned cpu;
-       /* When waiting for high frequency requests, e.g. during synchronous
-        * rendering split between the CPU and GPU, the finite amount of time
-        * required to set up the irq and wait upon it limits the response
-        * rate. By busywaiting on the request completion for a short while we
-        * can service the high frequency waits as quick as possible. However,
-        * if it is a slow request, we want to sleep as quickly as possible.
-        * The tradeoff between waiting and sleeping is roughly the time it
-        * takes to sleep on a request, on the order of a microsecond.
-        */
-       timeout_us += local_clock_us(&cpu);
-       do {
-               if (i915_gem_request_completed(req))
-                       return true;
-               if (signal_pending_state(state, current))
-                       break;
-               if (busywait_stop(timeout_us, cpu))
-                       break;
-               cpu_relax_lowlatency();
-       } while (!need_resched());
-       return false;
- }
- /**
-  * __i915_wait_request - wait until execution of request has finished
-  * @req: duh!
-  * @interruptible: do an interruptible wait (normally yes)
-  * @timeout: in - how long to wait (NULL forever); out - how much time remaining
-  * @rps: RPS client
-  *
-  * Note: It is of utmost importance that the passed in seqno and reset_counter
-  * values have been read by the caller in an smp safe manner. Where read-side
-  * locks are involved, it is sufficient to read the reset_counter before
-  * unlocking the lock that protects the seqno. For lockless tricks, the
-  * reset_counter _must_ be read before, and an appropriate smp_rmb must be
-  * inserted.
-  *
-  * Returns 0 if the request was found within the alloted time. Else returns the
-  * errno with remaining time filled in timeout argument.
-  */
- int __i915_wait_request(struct drm_i915_gem_request *req,
-                       bool interruptible,
-                       s64 *timeout,
-                       struct intel_rps_client *rps)
- {
-       int state = interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
-       DEFINE_WAIT(reset);
-       struct intel_wait wait;
-       unsigned long timeout_remain;
-       s64 before = 0; /* Only to silence a compiler warning. */
-       int ret = 0;
-       might_sleep();
-       if (list_empty(&req->list))
-               return 0;
-       if (i915_gem_request_completed(req))
-               return 0;
-       timeout_remain = MAX_SCHEDULE_TIMEOUT;
-       if (timeout) {
-               if (WARN_ON(*timeout < 0))
-                       return -EINVAL;
-               if (*timeout == 0)
-                       return -ETIME;
-               timeout_remain = nsecs_to_jiffies_timeout(*timeout);
-               /*
-                * Record current time in case interrupted by signal, or wedged.
-                */
-               before = ktime_get_raw_ns();
-       }
-       trace_i915_gem_request_wait_begin(req);
-       /* This client is about to stall waiting for the GPU. In many cases
-        * this is undesirable and limits the throughput of the system, as
-        * many clients cannot continue processing user input/output whilst
-        * blocked. RPS autotuning may take tens of milliseconds to respond
-        * to the GPU load and thus incurs additional latency for the client.
-        * We can circumvent that by promoting the GPU frequency to maximum
-        * before we wait. This makes the GPU throttle up much more quickly
-        * (good for benchmarks and user experience, e.g. window animations),
-        * but at a cost of spending more power processing the workload
-        * (bad for battery). Not all clients even want their results
-        * immediately and for them we should just let the GPU select its own
-        * frequency to maximise efficiency. To prevent a single client from
-        * forcing the clocks too high for the whole system, we only allow
-        * each client to waitboost once in a busy period.
-        */
-       if (INTEL_INFO(req->i915)->gen >= 6)
-               gen6_rps_boost(req->i915, rps, req->emitted_jiffies);
-       /* Optimistic spin for the next ~jiffie before touching IRQs */
-       if (i915_spin_request(req, state, 5))
-               goto complete;
-       set_current_state(state);
-       add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
-       intel_wait_init(&wait, req->seqno);
-       if (intel_engine_add_wait(req->engine, &wait))
-               /* In order to check that we haven't missed the interrupt
-                * as we enabled it, we need to kick ourselves to do a
-                * coherent check on the seqno before we sleep.
-                */
-               goto wakeup;
-       for (;;) {
-               if (signal_pending_state(state, current)) {
-                       ret = -ERESTARTSYS;
-                       break;
-               }
-               timeout_remain = io_schedule_timeout(timeout_remain);
-               if (timeout_remain == 0) {
-                       ret = -ETIME;
-                       break;
-               }
-               if (intel_wait_complete(&wait))
-                       break;
-               set_current_state(state);
- wakeup:
-               /* Carefully check if the request is complete, giving time
-                * for the seqno to be visible following the interrupt.
-                * We also have to check in case we are kicked by the GPU
-                * reset in order to drop the struct_mutex.
-                */
-               if (__i915_request_irq_complete(req))
-                       break;
-               /* Only spin if we know the GPU is processing this request */
-               if (i915_spin_request(req, state, 2))
-                       break;
-       }
-       remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
-       intel_engine_remove_wait(req->engine, &wait);
-       __set_current_state(TASK_RUNNING);
- complete:
-       trace_i915_gem_request_wait_end(req);
-       if (timeout) {
-               s64 tres = *timeout - (ktime_get_raw_ns() - before);
-               *timeout = tres < 0 ? 0 : tres;
-               /*
-                * Apparently ktime isn't accurate enough and occasionally has a
-                * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
-                * things up to make the test happy. We allow up to 1 jiffy.
-                *
-                * This is a regrssion from the timespec->ktime conversion.
-                */
-               if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
-                       *timeout = 0;
-       }
-       if (rps && req->seqno == req->engine->last_submitted_seqno) {
-               /* The GPU is now idle and this client has stalled.
-                * Since no other client has submitted a request in the
-                * meantime, assume that this client is the only one
-                * supplying work to the GPU but is unable to keep that
-                * work supplied because it is waiting. Since the GPU is
-                * then never kept fully busy, RPS autoclocking will
-                * keep the clocks relatively low, causing further delays.
-                * Compensate by giving the synchronous client credit for
-                * a waitboost next time.
-                */
-               spin_lock(&req->i915->rps.client_lock);
-               list_del_init(&rps->link);
-               spin_unlock(&req->i915->rps.client_lock);
-       }
-       return ret;
- }
- int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
-                                  struct drm_file *file)
- {
-       struct drm_i915_file_private *file_priv;
-       WARN_ON(!req || !file || req->file_priv);
-       if (!req || !file)
-               return -EINVAL;
-       if (req->file_priv)
-               return -EINVAL;
-       file_priv = file->driver_priv;
-       spin_lock(&file_priv->mm.lock);
-       req->file_priv = file_priv;
-       list_add_tail(&req->client_list, &file_priv->mm.request_list);
-       spin_unlock(&file_priv->mm.lock);
-       req->pid = get_pid(task_pid(current));
-       return 0;
- }
- static inline void
- i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
- {
-       struct drm_i915_file_private *file_priv = request->file_priv;
-       if (!file_priv)
-               return;
-       spin_lock(&file_priv->mm.lock);
-       list_del(&request->client_list);
-       request->file_priv = NULL;
-       spin_unlock(&file_priv->mm.lock);
-       put_pid(request->pid);
-       request->pid = NULL;
- }
- static void i915_gem_request_retire(struct drm_i915_gem_request *request)
- {
-       trace_i915_gem_request_retire(request);
-       /* We know the GPU must have read the request to have
-        * sent us the seqno + interrupt, so use the position
-        * of tail of the request to update the last known position
-        * of the GPU head.
-        *
-        * Note this requires that we are always called in request
-        * completion order.
-        */
-       request->ringbuf->last_retired_head = request->postfix;
-       list_del_init(&request->list);
-       i915_gem_request_remove_from_client(request);
-       if (request->previous_context) {
-               if (i915.enable_execlists)
-                       intel_lr_context_unpin(request->previous_context,
-                                              request->engine);
-       }
-       i915_gem_context_unreference(request->ctx);
-       i915_gem_request_unreference(request);
- }
- static void
- __i915_gem_request_retire__upto(struct drm_i915_gem_request *req)
- {
-       struct intel_engine_cs *engine = req->engine;
-       struct drm_i915_gem_request *tmp;
-       lockdep_assert_held(&engine->i915->drm.struct_mutex);
-       if (list_empty(&req->list))
-               return;
-       do {
-               tmp = list_first_entry(&engine->request_list,
-                                      typeof(*tmp), list);
-               i915_gem_request_retire(tmp);
-       } while (tmp != req);
-       WARN_ON(i915_verify_lists(engine->dev));
- }
- /**
-  * Waits for a request to be signaled, and cleans up the
-  * request and object lists appropriately for that event.
-  * @req: request to wait on
-  */
- int
- i915_wait_request(struct drm_i915_gem_request *req)
- {
-       struct drm_i915_private *dev_priv = req->i915;
-       bool interruptible;
-       int ret;
-       interruptible = dev_priv->mm.interruptible;
-       BUG_ON(!mutex_is_locked(&dev_priv->drm.struct_mutex));
-       ret = __i915_wait_request(req, interruptible, NULL, NULL);
-       if (ret)
-               return ret;
-       /* If the GPU hung, we want to keep the requests to find the guilty. */
-       if (!i915_reset_in_progress(&dev_priv->gpu_error))
-               __i915_gem_request_retire__upto(req);
-       return 0;
- }
- /**
-  * Ensures that all rendering to the object has completed and the object is
-  * safe to unbind from the GTT or access from the CPU.
-  * @obj: i915 gem object
-  * @readonly: waiting for read access or write
-  */
- int
- i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
-                              bool readonly)
- {
-       int ret, i;
-       if (!obj->active)
-               return 0;
-       if (readonly) {
-               if (obj->last_write_req != NULL) {
-                       ret = i915_wait_request(obj->last_write_req);
-                       if (ret)
-                               return ret;
-                       i = obj->last_write_req->engine->id;
-                       if (obj->last_read_req[i] == obj->last_write_req)
-                               i915_gem_object_retire__read(obj, i);
-                       else
-                               i915_gem_object_retire__write(obj);
-               }
-       } else {
-               for (i = 0; i < I915_NUM_ENGINES; i++) {
-                       if (obj->last_read_req[i] == NULL)
-                               continue;
-                       ret = i915_wait_request(obj->last_read_req[i]);
-                       if (ret)
-                               return ret;
-                       i915_gem_object_retire__read(obj, i);
-               }
-               GEM_BUG_ON(obj->active);
-       }
-       return 0;
- }
- static void
- i915_gem_object_retire_request(struct drm_i915_gem_object *obj,
-                              struct drm_i915_gem_request *req)
- {
-       int ring = req->engine->id;
-       if (obj->last_read_req[ring] == req)
-               i915_gem_object_retire__read(obj, ring);
-       else if (obj->last_write_req == req)
-               i915_gem_object_retire__write(obj);
-       if (!i915_reset_in_progress(&req->i915->gpu_error))
-               __i915_gem_request_retire__upto(req);
- }
- /* A nonblocking variant of the above wait. This is a highly dangerous routine
-  * as the object state may change during this call.
-  */
- static __must_check int
- i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
-                                           struct intel_rps_client *rps,
-                                           bool readonly)
- {
-       struct drm_device *dev = obj->base.dev;
-       struct drm_i915_private *dev_priv = to_i915(dev);
-       struct drm_i915_gem_request *requests[I915_NUM_ENGINES];
-       int ret, i, n = 0;
-       BUG_ON(!mutex_is_locked(&dev->struct_mutex));
-       BUG_ON(!dev_priv->mm.interruptible);
-       if (!obj->active)
-               return 0;
-       if (readonly) {
-               struct drm_i915_gem_request *req;
-               req = obj->last_write_req;
-               if (req == NULL)
-                       return 0;
-               requests[n++] = i915_gem_request_reference(req);
-       } else {
-               for (i = 0; i < I915_NUM_ENGINES; i++) {
-                       struct drm_i915_gem_request *req;
-                       req = obj->last_read_req[i];
-                       if (req == NULL)
-                               continue;
-                       requests[n++] = i915_gem_request_reference(req);
-               }
-       }
-       mutex_unlock(&dev->struct_mutex);
-       ret = 0;
-       for (i = 0; ret == 0 && i < n; i++)
-               ret = __i915_wait_request(requests[i], true, NULL, rps);
-       mutex_lock(&dev->struct_mutex);
-       for (i = 0; i < n; i++) {
-               if (ret == 0)
-                       i915_gem_object_retire_request(obj, requests[i]);
-               i915_gem_request_unreference(requests[i]);
-       }
-       return ret;
- }
- static struct intel_rps_client *to_rps_client(struct drm_file *file)
- {
-       struct drm_i915_file_private *fpriv = file->driver_priv;
-       return &fpriv->rps;
- }
- static enum fb_op_origin
- write_origin(struct drm_i915_gem_object *obj, unsigned domain)
- {
-       return domain == I915_GEM_DOMAIN_GTT && !obj->has_wc_mmap ?
-              ORIGIN_GTT : ORIGIN_CPU;
+       return (domain == I915_GEM_DOMAIN_GTT ?
+               obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
  }
  
  /**
@@@ -1831,10 -1520,7 +1520,7 @@@ i915_gem_set_domain_ioctl(struct drm_de
        int ret;
  
        /* Only handle setting domains to types used by the CPU. */
-       if (write_domain & I915_GEM_GPU_DOMAINS)
-               return -EINVAL;
-       if (read_domains & I915_GEM_GPU_DOMAINS)
+       if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
                return -EINVAL;
  
        /* Having something in the write domain implies it's in the read
        if (write_domain != 0 && read_domains != write_domain)
                return -EINVAL;
  
-       ret = i915_mutex_lock_interruptible(dev);
-       if (ret)
-               return ret;
-       obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
-       if (&obj->base == NULL) {
-               ret = -ENOENT;
-               goto unlock;
-       }
+       obj = i915_gem_object_lookup(file, args->handle);
+       if (!obj)
+               return -ENOENT;
  
        /* Try to flush the object off the GPU without holding the lock.
         * We will repeat the flush holding the lock in the normal manner
         * to catch cases where we are gazumped.
         */
-       ret = i915_gem_object_wait_rendering__nonblocking(obj,
-                                                         to_rps_client(file),
-                                                         !write_domain);
+       ret = __unsafe_wait_rendering(obj, to_rps_client(file), !write_domain);
        if (ret)
-               goto unref;
+               goto err;
+       ret = i915_mutex_lock_interruptible(dev);
+       if (ret)
+               goto err;
  
        if (read_domains & I915_GEM_DOMAIN_GTT)
                ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
        if (write_domain != 0)
                intel_fb_obj_invalidate(obj, write_origin(obj, write_domain));
  
- unref:
-       drm_gem_object_unreference(&obj->base);
- unlock:
+       i915_gem_object_put(obj);
        mutex_unlock(&dev->struct_mutex);
        return ret;
+ err:
+       i915_gem_object_put_unlocked(obj);
+       return ret;
  }
  
  /**
@@@ -1890,26 -1574,23 +1574,23 @@@ i915_gem_sw_finish_ioctl(struct drm_dev
  {
        struct drm_i915_gem_sw_finish *args = data;
        struct drm_i915_gem_object *obj;
-       int ret = 0;
-       ret = i915_mutex_lock_interruptible(dev);
-       if (ret)
-               return ret;
+       int err = 0;
  
-       obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
-       if (&obj->base == NULL) {
-               ret = -ENOENT;
-               goto unlock;
-       }
+       obj = i915_gem_object_lookup(file, args->handle);
+       if (!obj)
+               return -ENOENT;
  
        /* Pinned buffers may be scanout, so flush the cache */
-       if (obj->pin_display)
-               i915_gem_object_flush_cpu_write_domain(obj);
+       if (READ_ONCE(obj->pin_display)) {
+               err = i915_mutex_lock_interruptible(dev);
+               if (!err) {
+                       i915_gem_object_flush_cpu_write_domain(obj);
+                       mutex_unlock(&dev->struct_mutex);
+               }
+       }
  
-       drm_gem_object_unreference(&obj->base);
- unlock:
-       mutex_unlock(&dev->struct_mutex);
-       return ret;
+       i915_gem_object_put_unlocked(obj);
+       return err;
  }
  
  /**
@@@ -1937,7 -1618,7 +1618,7 @@@ i915_gem_mmap_ioctl(struct drm_device *
                    struct drm_file *file)
  {
        struct drm_i915_gem_mmap *args = data;
-       struct drm_gem_object *obj;
+       struct drm_i915_gem_object *obj;
        unsigned long addr;
  
        if (args->flags & ~(I915_MMAP_WC))
        if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
                return -ENODEV;
  
-       obj = drm_gem_object_lookup(file, args->handle);
-       if (obj == NULL)
+       obj = i915_gem_object_lookup(file, args->handle);
+       if (!obj)
                return -ENOENT;
  
        /* prime objects have no backing filp to GEM mmap
         * pages from.
         */
-       if (!obj->filp) {
-               drm_gem_object_unreference_unlocked(obj);
+       if (!obj->base.filp) {
+               i915_gem_object_put_unlocked(obj);
                return -EINVAL;
        }
  
-       addr = vm_mmap(obj->filp, 0, args->size,
+       addr = vm_mmap(obj->base.filp, 0, args->size,
                       PROT_READ | PROT_WRITE, MAP_SHARED,
                       args->offset);
        if (args->flags & I915_MMAP_WC) {
                struct vm_area_struct *vma;
  
                if (down_write_killable(&mm->mmap_sem)) {
-                       drm_gem_object_unreference_unlocked(obj);
+                       i915_gem_object_put_unlocked(obj);
                        return -EINTR;
                }
                vma = find_vma(mm, addr);
                up_write(&mm->mmap_sem);
  
                /* This may race, but that's ok, it only gets set */
-               WRITE_ONCE(to_intel_bo(obj)->has_wc_mmap, true);
+               WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
        }
-       drm_gem_object_unreference_unlocked(obj);
+       i915_gem_object_put_unlocked(obj);
        if (IS_ERR((void *)addr))
                return addr;
  
        return 0;
  }
  
+ static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
+ {
+       u64 size;
+       size = i915_gem_object_get_stride(obj);
+       size *= i915_gem_object_get_tiling(obj) == I915_TILING_Y ? 32 : 8;
+       return size >> PAGE_SHIFT;
+ }
+ /**
+  * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
+  *
+  * A history of the GTT mmap interface:
+  *
+  * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
+  *     aligned and suitable for fencing, and still fit into the available
+  *     mappable space left by the pinned display objects. A classic problem
+  *     we called the page-fault-of-doom where we would ping-pong between
+  *     two objects that could not fit inside the GTT and so the memcpy
+  *     would page one object in at the expense of the other between every
+  *     single byte.
+  *
+  * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
+  *     as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
+  *     object is too large for the available space (or simply too large
+  *     for the mappable aperture!), a view is created instead and faulted
+  *     into userspace. (This view is aligned and sized appropriately for
+  *     fenced access.)
+  *
+  * Restrictions:
+  *
+  *  * snoopable objects cannot be accessed via the GTT. It can cause machine
+  *    hangs on some architectures, corruption on others. An attempt to service
+  *    a GTT page fault from a snoopable object will generate a SIGBUS.
+  *
+  *  * the object must be able to fit into RAM (physical memory, though no
+  *    limited to the mappable aperture).
+  *
+  *
+  * Caveats:
+  *
+  *  * a new GTT page fault will synchronize rendering from the GPU and flush
+  *    all data to system memory. Subsequent access will not be synchronized.
+  *
+  *  * all mappings are revoked on runtime device suspend.
+  *
+  *  * there are only 8, 16 or 32 fence registers to share between all users
+  *    (older machines require fence register for display and blitter access
+  *    as well). Contention of the fence registers will cause the previous users
+  *    to be unmapped and any new access will generate new page faults.
+  *
+  *  * running out of memory while servicing a fault may generate a SIGBUS,
+  *    rather than the expected SIGSEGV.
+  */
+ int i915_gem_mmap_gtt_version(void)
+ {
+       return 1;
+ }
  /**
   * i915_gem_fault - fault a page into the GTT
-  * @vma: VMA in question
+  * @area: CPU VMA in question
   * @vmf: fault info
   *
   * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
   * from the GTT and/or fence registers to make room.  So performance may
   * suffer if the GTT working set is large or there are few fence registers
   * left.
+  *
+  * The current feature set supported by i915_gem_fault() and thus GTT mmaps
+  * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
   */
- int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
+ int i915_gem_fault(struct vm_area_struct *area, struct vm_fault *vmf)
  {
-       struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
+ #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
+       struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
        struct drm_device *dev = obj->base.dev;
        struct drm_i915_private *dev_priv = to_i915(dev);
        struct i915_ggtt *ggtt = &dev_priv->ggtt;
-       struct i915_ggtt_view view = i915_ggtt_view_normal;
-       pgoff_t page_offset;
-       unsigned long pfn;
-       int ret = 0;
        bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
-       intel_runtime_pm_get(dev_priv);
+       struct i915_vma *vma;
+       pgoff_t page_offset;
+       unsigned int flags;
+       int ret;
  
        /* We don't use vmf->pgoff since that has the fake offset */
-       page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
+       page_offset = ((unsigned long)vmf->virtual_address - area->vm_start) >>
                PAGE_SHIFT;
  
-       ret = i915_mutex_lock_interruptible(dev);
-       if (ret)
-               goto out;
        trace_i915_gem_object_fault(obj, page_offset, true, write);
  
        /* Try to flush the object off the GPU first without holding the lock.
-        * Upon reacquiring the lock, we will perform our sanity checks and then
+        * Upon acquiring the lock, we will perform our sanity checks and then
         * repeat the flush holding the lock in the normal manner to catch cases
         * where we are gazumped.
         */
-       ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
+       ret = __unsafe_wait_rendering(obj, NULL, !write);
        if (ret)
-               goto unlock;
+               goto err;
+       intel_runtime_pm_get(dev_priv);
+       ret = i915_mutex_lock_interruptible(dev);
+       if (ret)
+               goto err_rpm;
  
        /* Access to snoopable pages through the GTT is incoherent. */
        if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
                ret = -EFAULT;
-               goto unlock;
+               goto err_unlock;
        }
  
-       /* Use a partial view if the object is bigger than the aperture. */
-       if (obj->base.size >= ggtt->mappable_end &&
-           obj->tiling_mode == I915_TILING_NONE) {
-               static const unsigned int chunk_size = 256; // 1 MiB
+       /* If the object is smaller than a couple of partial vma, it is
+        * not worth only creating a single partial vma - we may as well
+        * clear enough space for the full object.
+        */
+       flags = PIN_MAPPABLE;
+       if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
+               flags |= PIN_NONBLOCK | PIN_NONFAULT;
+       /* Now pin it into the GTT as needed */
+       vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
+       if (IS_ERR(vma)) {
+               struct i915_ggtt_view view;
+               unsigned int chunk_size;
+               /* Use a partial view if it is bigger than available space */
+               chunk_size = MIN_CHUNK_PAGES;
+               if (i915_gem_object_is_tiled(obj))
+                       chunk_size = max(chunk_size, tile_row_pages(obj));
  
                memset(&view, 0, sizeof(view));
                view.type = I915_GGTT_VIEW_PARTIAL;
                view.params.partial.offset = rounddown(page_offset, chunk_size);
                view.params.partial.size =
-                       min_t(unsigned int,
-                             chunk_size,
-                             (vma->vm_end - vma->vm_start)/PAGE_SIZE -
+                       min_t(unsigned int, chunk_size,
+                             (area->vm_end - area->vm_start) / PAGE_SIZE -
                              view.params.partial.offset);
-       }
  
-       /* Now pin it into the GTT if needed */
-       ret = i915_gem_object_ggtt_pin(obj, &view, 0, PIN_MAPPABLE);
-       if (ret)
-               goto unlock;
+               /* If the partial covers the entire object, just create a
+                * normal VMA.
+                */
+               if (chunk_size >= obj->base.size >> PAGE_SHIFT)
+                       view.type = I915_GGTT_VIEW_NORMAL;
+               /* Userspace is now writing through an untracked VMA, abandon
+                * all hope that the hardware is able to track future writes.
+                */
+               obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
+               vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
+       }
+       if (IS_ERR(vma)) {
+               ret = PTR_ERR(vma);
+               goto err_unlock;
+       }
  
        ret = i915_gem_object_set_to_gtt_domain(obj, write);
        if (ret)
-               goto unpin;
+               goto err_unpin;
  
-       ret = i915_gem_object_get_fence(obj);
+       ret = i915_vma_get_fence(vma);
        if (ret)
-               goto unpin;
+               goto err_unpin;
  
        /* Finally, remap it using the new GTT offset */
-       pfn = ggtt->mappable_base +
-               i915_gem_obj_ggtt_offset_view(obj, &view);
-       pfn >>= PAGE_SHIFT;
-       if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) {
-               /* Overriding existing pages in partial view does not cause
-                * us any trouble as TLBs are still valid because the fault
-                * is due to userspace losing part of the mapping or never
-                * having accessed it before (at this partials' range).
-                */
-               unsigned long base = vma->vm_start +
-                                    (view.params.partial.offset << PAGE_SHIFT);
-               unsigned int i;
-               for (i = 0; i < view.params.partial.size; i++) {
-                       ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i);
-                       if (ret)
-                               break;
-               }
-               obj->fault_mappable = true;
-       } else {
-               if (!obj->fault_mappable) {
-                       unsigned long size = min_t(unsigned long,
-                                                  vma->vm_end - vma->vm_start,
-                                                  obj->base.size);
-                       int i;
-                       for (i = 0; i < size >> PAGE_SHIFT; i++) {
-                               ret = vm_insert_pfn(vma,
-                                                   (unsigned long)vma->vm_start + i * PAGE_SIZE,
-                                                   pfn + i);
-                               if (ret)
-                                       break;
-                       }
+       ret = remap_io_mapping(area,
+                              area->vm_start + (vma->ggtt_view.params.partial.offset << PAGE_SHIFT),
+                              (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
+                              min_t(u64, vma->size, area->vm_end - area->vm_start),
+                              &ggtt->mappable);
+       if (ret)
+               goto err_unpin;
  
-                       obj->fault_mappable = true;
-               } else
-                       ret = vm_insert_pfn(vma,
-                                           (unsigned long)vmf->virtual_address,
-                                           pfn + page_offset);
-       }
- unpin:
-       i915_gem_object_ggtt_unpin_view(obj, &view);
- unlock:
+       obj->fault_mappable = true;
+ err_unpin:
+       __i915_vma_unpin(vma);
+ err_unlock:
        mutex_unlock(&dev->struct_mutex);
- out:
+ err_rpm:
+       intel_runtime_pm_put(dev_priv);
+ err:
        switch (ret) {
        case -EIO:
                /*
                ret = VM_FAULT_SIGBUS;
                break;
        }
-       intel_runtime_pm_put(dev_priv);
        return ret;
  }
  
@@@ -2215,46 -1952,58 +1952,58 @@@ i915_gem_release_all_mmaps(struct drm_i
                i915_gem_release_mmap(obj);
  }
  
- uint32_t
- i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
+ /**
+  * i915_gem_get_ggtt_size - return required global GTT size for an object
+  * @dev_priv: i915 device
+  * @size: object size
+  * @tiling_mode: tiling mode
+  *
+  * Return the required global GTT size for an object, taking into account
+  * potential fence register mapping.
+  */
+ u64 i915_gem_get_ggtt_size(struct drm_i915_private *dev_priv,
+                          u64 size, int tiling_mode)
  {
-       uint32_t gtt_size;
+       u64 ggtt_size;
+       GEM_BUG_ON(size == 0);
  
-       if (INTEL_INFO(dev)->gen >= 4 ||
+       if (INTEL_GEN(dev_priv) >= 4 ||
            tiling_mode == I915_TILING_NONE)
                return size;
  
        /* Previous chips need a power-of-two fence region when tiling */
-       if (IS_GEN3(dev))
-               gtt_size = 1024*1024;
+       if (IS_GEN3(dev_priv))
+               ggtt_size = 1024*1024;
        else
-               gtt_size = 512*1024;
+               ggtt_size = 512*1024;
  
-       while (gtt_size < size)
-               gtt_size <<= 1;
+       while (ggtt_size < size)
+               ggtt_size <<= 1;
  
-       return gtt_size;
+       return ggtt_size;
  }
  
  /**
-  * i915_gem_get_gtt_alignment - return required GTT alignment for an object
-  * @dev: drm device
+  * i915_gem_get_ggtt_alignment - return required global GTT alignment
+  * @dev_priv: i915 device
   * @size: object size
   * @tiling_mode: tiling mode
-  * @fenced: is fenced alignemned required or not
+  * @fenced: is fenced alignment required or not
   *
-  * Return the required GTT alignment for an object, taking into account
+  * Return the required global GTT alignment for an object, taking into account
   * potential fence register mapping.
   */
- uint32_t
- i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
-                          int tiling_mode, bool fenced)
+ u64 i915_gem_get_ggtt_alignment(struct drm_i915_private *dev_priv, u64 size,
+                               int tiling_mode, bool fenced)
  {
+       GEM_BUG_ON(size == 0);
        /*
         * Minimum alignment is 4k (GTT page size), but might be greater
         * if a fence register is needed for the object.
         */
-       if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
+       if (INTEL_GEN(dev_priv) >= 4 || (!fenced && IS_G33(dev_priv)) ||
            tiling_mode == I915_TILING_NONE)
                return 4096;
  
         * Previous chips need to be aligned to the size of the smallest
         * fence register that can contain the object.
         */
-       return i915_gem_get_gtt_size(dev, size, tiling_mode);
+       return i915_gem_get_ggtt_size(dev_priv, size, tiling_mode);
  }
  
  static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  {
        struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
-       int ret;
-       dev_priv->mm.shrinker_no_lock_stealing = true;
+       int err;
  
-       ret = drm_gem_create_mmap_offset(&obj->base);
-       if (ret != -ENOSPC)
-               goto out;
+       err = drm_gem_create_mmap_offset(&obj->base);
+       if (!err)
+               return 0;
  
-       /* Badly fragmented mmap space? The only way we can recover
-        * space is by destroying unwanted objects. We can't randomly release
-        * mmap_offsets as userspace expects them to be persistent for the
-        * lifetime of the objects. The closest we can is to release the
-        * offsets on purgeable objects by truncating it and marking it purged,
-        * which prevents userspace from ever using that object again.
+       /* We can idle the GPU locklessly to flush stale objects, but in order
+        * to claim that space for ourselves, we need to take the big
+        * struct_mutex to free the requests+objects and allocate our slot.
         */
-       i915_gem_shrink(dev_priv,
-                       obj->base.size >> PAGE_SHIFT,
-                       I915_SHRINK_BOUND |
-                       I915_SHRINK_UNBOUND |
-                       I915_SHRINK_PURGEABLE);
-       ret = drm_gem_create_mmap_offset(&obj->base);
-       if (ret != -ENOSPC)
-               goto out;
+       err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
+       if (err)
+               return err;
  
-       i915_gem_shrink_all(dev_priv);
-       ret = drm_gem_create_mmap_offset(&obj->base);
- out:
-       dev_priv->mm.shrinker_no_lock_stealing = false;
+       err = i915_mutex_lock_interruptible(&dev_priv->drm);
+       if (!err) {
+               i915_gem_retire_requests(dev_priv);
+               err = drm_gem_create_mmap_offset(&obj->base);
+               mutex_unlock(&dev_priv->drm.struct_mutex);
+       }
  
-       return ret;
+       return err;
  }
  
  static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
@@@ -2314,32 -2055,15 +2055,15 @@@ i915_gem_mmap_gtt(struct drm_file *file
        struct drm_i915_gem_object *obj;
        int ret;
  
-       ret = i915_mutex_lock_interruptible(dev);
-       if (ret)
-               return ret;
-       obj = to_intel_bo(drm_gem_object_lookup(file, handle));
-       if (&obj->base == NULL) {
-               ret = -ENOENT;
-               goto unlock;
-       }
-       if (obj->madv != I915_MADV_WILLNEED) {
-               DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
-               ret = -EFAULT;
-               goto out;
-       }
+       obj = i915_gem_object_lookup(file, handle);
+       if (!obj)
+               return -ENOENT;
  
        ret = i915_gem_object_create_mmap_offset(obj);
-       if (ret)
-               goto out;
+       if (ret == 0)
+               *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  
-       *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
- out:
-       drm_gem_object_unreference(&obj->base);
- unlock:
-       mutex_unlock(&dev->struct_mutex);
+       i915_gem_object_put_unlocked(obj);
        return ret;
  }
  
@@@ -2457,7 -2181,7 +2181,7 @@@ i915_gem_object_put_pages(struct drm_i9
        if (obj->pages_pin_count)
                return -EBUSY;
  
-       BUG_ON(i915_gem_obj_bound_any(obj));
+       GEM_BUG_ON(obj->bind_count);
  
        /* ->put_pages might need to allocate memory for the bit17 swizzle
         * array, hence protect them from being reaped by removing them from gtt
        list_del(&obj->global_list);
  
        if (obj->mapping) {
-               if (is_vmalloc_addr(obj->mapping))
-                       vunmap(obj->mapping);
+               void *ptr;
+               ptr = ptr_mask_bits(obj->mapping);
+               if (is_vmalloc_addr(ptr))
+                       vunmap(ptr);
                else
-                       kunmap(kmap_to_page(obj->mapping));
+                       kunmap(kmap_to_page(ptr));
                obj->mapping = NULL;
        }
  
@@@ -2577,7 -2305,7 +2305,7 @@@ i915_gem_object_get_pages_gtt(struct dr
        if (i915_gem_object_needs_bit17_swizzle(obj))
                i915_gem_object_do_bit_17_swizzle(obj);
  
-       if (obj->tiling_mode != I915_TILING_NONE &&
+       if (i915_gem_object_is_tiled(obj) &&
            dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
                i915_gem_object_pin_pages(obj);
  
@@@ -2641,7 -2369,8 +2369,8 @@@ i915_gem_object_get_pages(struct drm_i9
  }
  
  /* The 'mapping' part of i915_gem_object_pin_map() below */
- static void *i915_gem_object_map(const struct drm_i915_gem_object *obj)
+ static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
+                                enum i915_map_type type)
  {
        unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
        struct sg_table *sgt = obj->pages;
        struct page *stack_pages[32];
        struct page **pages = stack_pages;
        unsigned long i = 0;
+       pgprot_t pgprot;
        void *addr;
  
        /* A single page can always be kmapped */
-       if (n_pages == 1)
+       if (n_pages == 1 && type == I915_MAP_WB)
                return kmap(sg_page(sgt->sgl));
  
        if (n_pages > ARRAY_SIZE(stack_pages)) {
        /* Check that we have the expected number of pages */
        GEM_BUG_ON(i != n_pages);
  
-       addr = vmap(pages, n_pages, 0, PAGE_KERNEL);
+       switch (type) {
+       case I915_MAP_WB:
+               pgprot = PAGE_KERNEL;
+               break;
+       case I915_MAP_WC:
+               pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
+               break;
+       }
+       addr = vmap(pages, n_pages, 0, pgprot);
  
        if (pages != stack_pages)
                drm_free_large(pages);
  }
  
  /* get, pin, and map the pages of the object into kernel space */
- void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj)
+ void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
+                             enum i915_map_type type)
  {
+       enum i915_map_type has_type;
+       bool pinned;
+       void *ptr;
        int ret;
  
        lockdep_assert_held(&obj->base.dev->struct_mutex);
+       GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
  
        ret = i915_gem_object_get_pages(obj);
        if (ret)
                return ERR_PTR(ret);
  
        i915_gem_object_pin_pages(obj);
+       pinned = obj->pages_pin_count > 1;
  
-       if (!obj->mapping) {
-               obj->mapping = i915_gem_object_map(obj);
-               if (!obj->mapping) {
-                       i915_gem_object_unpin_pages(obj);
-                       return ERR_PTR(-ENOMEM);
+       ptr = ptr_unpack_bits(obj->mapping, has_type);
+       if (ptr && has_type != type) {
+               if (pinned) {
+                       ret = -EBUSY;
+                       goto err;
                }
-       }
-       return obj->mapping;
- }
  
- void i915_vma_move_to_active(struct i915_vma *vma,
-                            struct drm_i915_gem_request *req)
- {
-       struct drm_i915_gem_object *obj = vma->obj;
-       struct intel_engine_cs *engine;
-       engine = i915_gem_request_get_engine(req);
-       /* Add a reference if we're newly entering the active list. */
-       if (obj->active == 0)
-               drm_gem_object_reference(&obj->base);
-       obj->active |= intel_engine_flag(engine);
-       list_move_tail(&obj->engine_list[engine->id], &engine->active_list);
-       i915_gem_request_assign(&obj->last_read_req[engine->id], req);
-       list_move_tail(&vma->vm_link, &vma->vm->active_list);
- }
- static void
- i915_gem_object_retire__write(struct drm_i915_gem_object *obj)
- {
-       GEM_BUG_ON(obj->last_write_req == NULL);
-       GEM_BUG_ON(!(obj->active & intel_engine_flag(obj->last_write_req->engine)));
-       i915_gem_request_assign(&obj->last_write_req, NULL);
-       intel_fb_obj_flush(obj, true, ORIGIN_CS);
- }
- static void
- i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring)
- {
-       struct i915_vma *vma;
-       GEM_BUG_ON(obj->last_read_req[ring] == NULL);
-       GEM_BUG_ON(!(obj->active & (1 << ring)));
-       list_del_init(&obj->engine_list[ring]);
-       i915_gem_request_assign(&obj->last_read_req[ring], NULL);
-       if (obj->last_write_req && obj->last_write_req->engine->id == ring)
-               i915_gem_object_retire__write(obj);
-       obj->active &= ~(1 << ring);
-       if (obj->active)
-               return;
-       /* Bump our place on the bound list to keep it roughly in LRU order
-        * so that we don't steal from recently used but inactive objects
-        * (unless we are forced to ofc!)
-        */
-       list_move_tail(&obj->global_list,
-                      &to_i915(obj->base.dev)->mm.bound_list);
-       list_for_each_entry(vma, &obj->vma_list, obj_link) {
-               if (!list_empty(&vma->vm_link))
-                       list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
-       }
-       i915_gem_request_assign(&obj->last_fenced_req, NULL);
-       drm_gem_object_unreference(&obj->base);
- }
- static int
- i915_gem_init_seqno(struct drm_i915_private *dev_priv, u32 seqno)
- {
-       struct intel_engine_cs *engine;
-       int ret;
-       /* Carefully retire all requests without writing to the rings */
-       for_each_engine(engine, dev_priv) {
-               ret = intel_engine_idle(engine);
-               if (ret)
-                       return ret;
-       }
-       i915_gem_retire_requests(dev_priv);
+               if (is_vmalloc_addr(ptr))
+                       vunmap(ptr);
+               else
+                       kunmap(kmap_to_page(ptr));
  
-       /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
-       if (!i915_seqno_passed(seqno, dev_priv->next_seqno)) {
-               while (intel_kick_waiters(dev_priv) ||
-                      intel_kick_signalers(dev_priv))
-                       yield();
+               ptr = obj->mapping = NULL;
        }
  
-       /* Finally reset hw state */
-       for_each_engine(engine, dev_priv)
-               intel_ring_init_seqno(engine, seqno);
-       return 0;
- }
- int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
- {
-       struct drm_i915_private *dev_priv = to_i915(dev);
-       int ret;
-       if (seqno == 0)
-               return -EINVAL;
-       /* HWS page needs to be set less than what we
-        * will inject to ring
-        */
-       ret = i915_gem_init_seqno(dev_priv, seqno - 1);
-       if (ret)
-               return ret;
-       /* Carefully set the last_seqno value so that wrap
-        * detection still works
-        */
-       dev_priv->next_seqno = seqno;
-       dev_priv->last_seqno = seqno - 1;
-       if (dev_priv->last_seqno == 0)
-               dev_priv->last_seqno--;
-       return 0;
- }
- int
- i915_gem_get_seqno(struct drm_i915_private *dev_priv, u32 *seqno)
- {
-       /* reserve 0 for non-seqno */
-       if (dev_priv->next_seqno == 0) {
-               int ret = i915_gem_init_seqno(dev_priv, 0);
-               if (ret)
-                       return ret;
+       if (!ptr) {
+               ptr = i915_gem_object_map(obj, type);
+               if (!ptr) {
+                       ret = -ENOMEM;
+                       goto err;
+               }
  
-               dev_priv->next_seqno = 1;
+               obj->mapping = ptr_pack_bits(ptr, type);
        }
  
-       *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
-       return 0;
- }
- static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
- {
-       struct drm_i915_private *dev_priv = engine->i915;
-       dev_priv->gt.active_engines |= intel_engine_flag(engine);
-       if (dev_priv->gt.awake)
-               return;
-       intel_runtime_pm_get_noresume(dev_priv);
-       dev_priv->gt.awake = true;
-       i915_update_gfx_val(dev_priv);
-       if (INTEL_GEN(dev_priv) >= 6)
-               gen6_rps_busy(dev_priv);
+       return ptr;
  
-       queue_delayed_work(dev_priv->wq,
-                          &dev_priv->gt.retire_work,
-                          round_jiffies_up_relative(HZ));
+ err:
+       i915_gem_object_unpin_pages(obj);
+       return ERR_PTR(ret);
  }
  
- /*
-  * NB: This function is not allowed to fail. Doing so would mean the the
-  * request is not being tracked for completion but the work itself is
-  * going to happen on the hardware. This would be a Bad Thing(tm).
-  */
- void __i915_add_request(struct drm_i915_gem_request *request,
-                       struct drm_i915_gem_object *obj,
-                       bool flush_caches)
+ static void
+ i915_gem_object_retire__write(struct i915_gem_active *active,
+                             struct drm_i915_gem_request *request)
  {
-       struct intel_engine_cs *engine;
-       struct intel_ringbuffer *ringbuf;
-       u32 request_start;
-       u32 reserved_tail;
-       int ret;
-       if (WARN_ON(request == NULL))
-               return;
-       engine = request->engine;
-       ringbuf = request->ringbuf;
-       /*
-        * To ensure that this call will not fail, space for its emissions
-        * should already have been reserved in the ring buffer. Let the ring
-        * know that it is time to use that space up.
-        */
-       request_start = intel_ring_get_tail(ringbuf);
-       reserved_tail = request->reserved_space;
-       request->reserved_space = 0;
-       /*
-        * Emit any outstanding flushes - execbuf can fail to emit the flush
-        * after having emitted the batchbuffer command. Hence we need to fix
-        * things up similar to emitting the lazy request. The difference here
-        * is that the flush _must_ happen before the next request, no matter
-        * what.
-        */
-       if (flush_caches) {
-               if (i915.enable_execlists)
-                       ret = logical_ring_flush_all_caches(request);
-               else
-                       ret = intel_ring_flush_all_caches(request);
-               /* Not allowed to fail! */
-               WARN(ret, "*_ring_flush_all_caches failed: %d!\n", ret);
-       }
-       trace_i915_gem_request_add(request);
+       struct drm_i915_gem_object *obj =
+               container_of(active, struct drm_i915_gem_object, last_write);
  
-       request->head = request_start;
+       intel_fb_obj_flush(obj, true, ORIGIN_CS);
+ }
  
-       /* Whilst this request exists, batch_obj will be on the
-        * active_list, and so will hold the active reference. Only when this
-        * request is retired will the the batch_obj be moved onto the
-        * inactive_list and lose its active reference. Hence we do not need
-        * to explicitly hold another reference here.
-        */
-       request->batch_obj = obj;
+ static void
+ i915_gem_object_retire__read(struct i915_gem_active *active,
+                            struct drm_i915_gem_request *request)
+ {
+       int idx = request->engine->id;
+       struct drm_i915_gem_object *obj =
+               container_of(active, struct drm_i915_gem_object, last_read[idx]);
  
-       /* Seal the request and mark it as pending execution. Note that
-        * we may inspect this state, without holding any locks, during
-        * hangcheck. Hence we apply the barrier to ensure that we do not
-        * see a more recent value in the hws than we are tracking.
-        */
-       request->emitted_jiffies = jiffies;
-       request->previous_seqno = engine->last_submitted_seqno;
-       smp_store_mb(engine->last_submitted_seqno, request->seqno);
-       list_add_tail(&request->list, &engine->request_list);
-       /* Record the position of the start of the request so that
-        * should we detect the updated seqno part-way through the
-        * GPU processing the request, we never over-estimate the
-        * position of the head.
-        */
-       request->postfix = intel_ring_get_tail(ringbuf);
+       GEM_BUG_ON(!i915_gem_object_has_active_engine(obj, idx));
  
-       if (i915.enable_execlists)
-               ret = engine->emit_request(request);
-       else {
-               ret = engine->add_request(request);
+       i915_gem_object_clear_active(obj, idx);
+       if (i915_gem_object_is_active(obj))
+               return;
  
-               request->tail = intel_ring_get_tail(ringbuf);
-       }
-       /* Not allowed to fail! */
-       WARN(ret, "emit|add_request failed: %d!\n", ret);
-       /* Sanity check that the reserved size was large enough. */
-       ret = intel_ring_get_tail(ringbuf) - request_start;
-       if (ret < 0)
-               ret += ringbuf->size;
-       WARN_ONCE(ret > reserved_tail,
-                 "Not enough space reserved (%d bytes) "
-                 "for adding the request (%d bytes)\n",
-                 reserved_tail, ret);
+       /* Bump our place on the bound list to keep it roughly in LRU order
+        * so that we don't steal from recently used but inactive objects
+        * (unless we are forced to ofc!)
+        */
+       if (obj->bind_count)
+               list_move_tail(&obj->global_list,
+                              &request->i915->mm.bound_list);
  
-       i915_gem_mark_busy(engine);
+       i915_gem_object_put(obj);
  }
  
  static bool i915_context_is_banned(const struct i915_gem_context *ctx)
@@@ -2981,101 -2532,6 +2532,6 @@@ static void i915_set_reset_status(struc
        }
  }
  
- void i915_gem_request_free(struct kref *req_ref)
- {
-       struct drm_i915_gem_request *req = container_of(req_ref,
-                                                typeof(*req), ref);
-       kmem_cache_free(req->i915->requests, req);
- }
- static inline int
- __i915_gem_request_alloc(struct intel_engine_cs *engine,
-                        struct i915_gem_context *ctx,
-                        struct drm_i915_gem_request **req_out)
- {
-       struct drm_i915_private *dev_priv = engine->i915;
-       unsigned reset_counter = i915_reset_counter(&dev_priv->gpu_error);
-       struct drm_i915_gem_request *req;
-       int ret;
-       if (!req_out)
-               return -EINVAL;
-       *req_out = NULL;
-       /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
-        * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
-        * and restart.
-        */
-       ret = i915_gem_check_wedge(reset_counter, dev_priv->mm.interruptible);
-       if (ret)
-               return ret;
-       req = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL);
-       if (req == NULL)
-               return -ENOMEM;
-       ret = i915_gem_get_seqno(engine->i915, &req->seqno);
-       if (ret)
-               goto err;
-       kref_init(&req->ref);
-       req->i915 = dev_priv;
-       req->engine = engine;
-       req->ctx  = ctx;
-       i915_gem_context_reference(req->ctx);
-       /*
-        * Reserve space in the ring buffer for all the commands required to
-        * eventually emit this request. This is to guarantee that the
-        * i915_add_request() call can't fail. Note that the reserve may need
-        * to be redone if the request is not actually submitted straight
-        * away, e.g. because a GPU scheduler has deferred it.
-        */
-       req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
-       if (i915.enable_execlists)
-               ret = intel_logical_ring_alloc_request_extras(req);
-       else
-               ret = intel_ring_alloc_request_extras(req);
-       if (ret)
-               goto err_ctx;
-       *req_out = req;
-       return 0;
- err_ctx:
-       i915_gem_context_unreference(ctx);
- err:
-       kmem_cache_free(dev_priv->requests, req);
-       return ret;
- }
- /**
-  * i915_gem_request_alloc - allocate a request structure
-  *
-  * @engine: engine that we wish to issue the request on.
-  * @ctx: context that the request will be associated with.
-  *       This can be NULL if the request is not directly related to
-  *       any specific user context, in which case this function will
-  *       choose an appropriate context to use.
-  *
-  * Returns a pointer to the allocated request if successful,
-  * or an error code if not.
-  */
- struct drm_i915_gem_request *
- i915_gem_request_alloc(struct intel_engine_cs *engine,
-                      struct i915_gem_context *ctx)
- {
-       struct drm_i915_gem_request *req;
-       int err;
-       if (ctx == NULL)
-               ctx = engine->i915->kernel_context;
-       err = __i915_gem_request_alloc(engine, ctx, &req);
-       return err ? ERR_PTR(err) : req;
- }
  struct drm_i915_gem_request *
  i915_gem_find_active_request(struct intel_engine_cs *engine)
  {
         * extra delay for a recent interrupt is pointless. Hence, we do
         * not need an engine->irq_seqno_barrier() before the seqno reads.
         */
-       list_for_each_entry(request, &engine->request_list, list) {
+       list_for_each_entry(request, &engine->request_list, link) {
                if (i915_gem_request_completed(request))
                        continue;
  
+               if (!i915_sw_fence_done(&request->submit))
+                       break;
                return request;
        }
  
        return NULL;
  }
  
- static void i915_gem_reset_engine_status(struct intel_engine_cs *engine)
+ static void reset_request(struct drm_i915_gem_request *request)
+ {
+       void *vaddr = request->ring->vaddr;
+       u32 head;
+       /* As this request likely depends on state from the lost
+        * context, clear out all the user operations leaving the
+        * breadcrumb at the end (so we get the fence notifications).
+        */
+       head = request->head;
+       if (request->postfix < head) {
+               memset(vaddr + head, 0, request->ring->size - head);
+               head = 0;
+       }
+       memset(vaddr + head, 0, request->postfix - head);
+ }
+ static void i915_gem_reset_engine(struct intel_engine_cs *engine)
  {
        struct drm_i915_gem_request *request;
+       struct i915_gem_context *incomplete_ctx;
        bool ring_hung;
  
+       /* Ensure irq handler finishes, and not run again. */
+       tasklet_kill(&engine->irq_tasklet);
+       if (engine->irq_seqno_barrier)
+               engine->irq_seqno_barrier(engine);
        request = i915_gem_find_active_request(engine);
-       if (request == NULL)
+       if (!request)
                return;
  
        ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
        i915_set_reset_status(request->ctx, ring_hung);
-       list_for_each_entry_continue(request, &engine->request_list, list)
-               i915_set_reset_status(request->ctx, false);
- }
- static void i915_gem_reset_engine_cleanup(struct intel_engine_cs *engine)
- {
-       struct intel_ringbuffer *buffer;
-       while (!list_empty(&engine->active_list)) {
-               struct drm_i915_gem_object *obj;
-               obj = list_first_entry(&engine->active_list,
-                                      struct drm_i915_gem_object,
-                                      engine_list[engine->id]);
-               i915_gem_object_retire__read(obj, engine->id);
-       }
-       /*
-        * Clear the execlists queue up before freeing the requests, as those
-        * are the ones that keep the context and ringbuffer backing objects
-        * pinned in place.
-        */
-       if (i915.enable_execlists) {
-               /* Ensure irq handler finishes or is cancelled. */
-               tasklet_kill(&engine->irq_tasklet);
-               intel_execlists_cancel_requests(engine);
-       }
-       /*
-        * We must free the requests after all the corresponding objects have
-        * been moved off active lists. Which is the same order as the normal
-        * retire_requests function does. This is important if object hold
-        * implicit references on things like e.g. ppgtt address spaces through
-        * the request.
-        */
-       while (!list_empty(&engine->request_list)) {
-               struct drm_i915_gem_request *request;
+       if (!ring_hung)
+               return;
  
-               request = list_first_entry(&engine->request_list,
-                                          struct drm_i915_gem_request,
-                                          list);
+       DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
+                        engine->name, request->fence.seqno);
  
-               i915_gem_request_retire(request);
-       }
+       /* Setup the CS to resume from the breadcrumb of the hung request */
+       engine->reset_hw(engine, request);
  
-       /* Having flushed all requests from all queues, we know that all
-        * ringbuffers must now be empty. However, since we do not reclaim
-        * all space when retiring the request (to prevent HEADs colliding
-        * with rapid ringbuffer wraparound) the amount of available space
-        * upon reset is less than when we start. Do one more pass over
-        * all the ringbuffers to reset last_retired_head.
+       /* Users of the default context do not rely on logical state
+        * preserved between batches. They have to emit full state on
+        * every batch and so it is safe to execute queued requests following
+        * the hang.
+        *
+        * Other contexts preserve state, now corrupt. We want to skip all
+        * queued requests that reference the corrupt context.
         */
-       list_for_each_entry(buffer, &engine->buffers, link) {
-               buffer->last_retired_head = buffer->tail;
-               intel_ring_update_space(buffer);
-       }
-       intel_ring_init_seqno(engine, engine->last_submitted_seqno);
+       incomplete_ctx = request->ctx;
+       if (i915_gem_context_is_default(incomplete_ctx))
+               return;
  
-       engine->i915->gt.active_engines &= ~intel_engine_flag(engine);
+       list_for_each_entry_continue(request, &engine->request_list, link)
+               if (request->ctx == incomplete_ctx)
+                       reset_request(request);
  }
  
- void i915_gem_reset(struct drm_device *dev)
+ void i915_gem_reset(struct drm_i915_private *dev_priv)
  {
-       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_engine_cs *engine;
  
-       /*
-        * Before we free the objects from the requests, we need to inspect
-        * them for finding the guilty party. As the requests only borrow
-        * their reference to the objects, the inspection must be done first.
-        */
-       for_each_engine(engine, dev_priv)
-               i915_gem_reset_engine_status(engine);
+       i915_gem_retire_requests(dev_priv);
  
        for_each_engine(engine, dev_priv)
-               i915_gem_reset_engine_cleanup(engine);
-       mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
+               i915_gem_reset_engine(engine);
  
-       i915_gem_context_reset(dev);
+       i915_gem_restore_fences(&dev_priv->drm);
  
-       i915_gem_restore_fences(dev);
+       if (dev_priv->gt.awake) {
+               intel_sanitize_gt_powersave(dev_priv);
+               intel_enable_gt_powersave(dev_priv);
+               if (INTEL_GEN(dev_priv) >= 6)
+                       gen6_rps_busy(dev_priv);
+       }
+ }
  
-       WARN_ON(i915_verify_lists(dev));
+ static void nop_submit_request(struct drm_i915_gem_request *request)
+ {
  }
  
- /**
-  * This function clears the request list as sequence numbers are passed.
-  * @engine: engine to retire requests on
-  */
- void
- i915_gem_retire_requests_ring(struct intel_engine_cs *engine)
+ static void i915_gem_cleanup_engine(struct intel_engine_cs *engine)
  {
-       WARN_ON(i915_verify_lists(engine->dev));
+       engine->submit_request = nop_submit_request;
  
-       /* Retire requests first as we use it above for the early return.
-        * If we retire requests last, we may use a later seqno and so clear
-        * the requests lists without clearing the active list, leading to
-        * confusion.
+       /* Mark all pending requests as complete so that any concurrent
+        * (lockless) lookup doesn't try and wait upon the request as we
+        * reset it.
         */
-       while (!list_empty(&engine->request_list)) {
-               struct drm_i915_gem_request *request;
-               request = list_first_entry(&engine->request_list,
-                                          struct drm_i915_gem_request,
-                                          list);
-               if (!i915_gem_request_completed(request))
-                       break;
-               i915_gem_request_retire(request);
-       }
+       intel_engine_init_seqno(engine, engine->last_submitted_seqno);
  
-       /* Move any buffers on the active list that are no longer referenced
-        * by the ringbuffer to the flushing/inactive lists as appropriate,
-        * before we free the context associated with the requests.
+       /*
+        * Clear the execlists queue up before freeing the requests, as those
+        * are the ones that keep the context and ringbuffer backing objects
+        * pinned in place.
         */
-       while (!list_empty(&engine->active_list)) {
-               struct drm_i915_gem_object *obj;
  
-               obj = list_first_entry(&engine->active_list,
-                                      struct drm_i915_gem_object,
-                                      engine_list[engine->id]);
-               if (!list_empty(&obj->last_read_req[engine->id]->list))
-                       break;
-               i915_gem_object_retire__read(obj, engine->id);
+       if (i915.enable_execlists) {
+               spin_lock(&engine->execlist_lock);
+               INIT_LIST_HEAD(&engine->execlist_queue);
+               i915_gem_request_put(engine->execlist_port[0].request);
+               i915_gem_request_put(engine->execlist_port[1].request);
+               memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
+               spin_unlock(&engine->execlist_lock);
        }
  
-       WARN_ON(i915_verify_lists(engine->dev));
+       engine->i915->gt.active_engines &= ~intel_engine_flag(engine);
  }
  
- void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
+ void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
  {
        struct intel_engine_cs *engine;
  
        lockdep_assert_held(&dev_priv->drm.struct_mutex);
+       set_bit(I915_WEDGED, &dev_priv->gpu_error.flags);
  
-       if (dev_priv->gt.active_engines == 0)
-               return;
-       GEM_BUG_ON(!dev_priv->gt.awake);
-       for_each_engine(engine, dev_priv) {
-               i915_gem_retire_requests_ring(engine);
-               if (list_empty(&engine->request_list))
-                       dev_priv->gt.active_engines &= ~intel_engine_flag(engine);
-       }
+       i915_gem_context_lost(dev_priv);
+       for_each_engine(engine, dev_priv)
+               i915_gem_cleanup_engine(engine);
+       mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
  
-       if (dev_priv->gt.active_engines == 0)
-               queue_delayed_work(dev_priv->wq,
-                                  &dev_priv->gt.idle_work,
-                                  msecs_to_jiffies(100));
+       i915_gem_retire_requests(dev_priv);
  }
  
  static void
@@@ -3287,10 -2701,12 +2701,12 @@@ i915_gem_retire_work_handler(struct wor
         * We do not need to do this test under locking as in the worst-case
         * we queue the retire worker once too often.
         */
-       if (READ_ONCE(dev_priv->gt.awake))
+       if (READ_ONCE(dev_priv->gt.awake)) {
+               i915_queue_hangcheck(dev_priv);
                queue_delayed_work(dev_priv->wq,
                                   &dev_priv->gt.retire_work,
                                   round_jiffies_up_relative(HZ));
+       }
  }
  
  static void
@@@ -3300,7 -2716,6 +2716,6 @@@ i915_gem_idle_work_handler(struct work_
                container_of(work, typeof(*dev_priv), gt.idle_work.work);
        struct drm_device *dev = &dev_priv->drm;
        struct intel_engine_cs *engine;
-       unsigned int stuck_engines;
        bool rearm_hangcheck;
  
        if (!READ_ONCE(dev_priv->gt.awake))
        }
  
        if (dev_priv->gt.active_engines)
-               goto out_unlock;
-       for_each_engine(engine, dev_priv)
-               i915_gem_batch_pool_fini(&engine->batch_pool);
-       GEM_BUG_ON(!dev_priv->gt.awake);
-       dev_priv->gt.awake = false;
-       rearm_hangcheck = false;
-       stuck_engines = intel_kick_waiters(dev_priv);
-       if (unlikely(stuck_engines)) {
-               DRM_DEBUG_DRIVER("kicked stuck waiters...missed irq\n");
-               dev_priv->gpu_error.missed_irq_rings |= stuck_engines;
-       }
-       if (INTEL_GEN(dev_priv) >= 6)
-               gen6_rps_idle(dev_priv);
-       intel_runtime_pm_put(dev_priv);
- out_unlock:
-       mutex_unlock(&dev->struct_mutex);
- out_rearm:
-       if (rearm_hangcheck) {
-               GEM_BUG_ON(!dev_priv->gt.awake);
-               i915_queue_hangcheck(dev_priv);
-       }
- }
- /**
-  * Ensures that an object will eventually get non-busy by flushing any required
-  * write domains, emitting any outstanding lazy request and retiring and
-  * completed requests.
-  * @obj: object to flush
-  */
- static int
- i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
- {
-       int i;
-       if (!obj->active)
-               return 0;
-       for (i = 0; i < I915_NUM_ENGINES; i++) {
-               struct drm_i915_gem_request *req;
-               req = obj->last_read_req[i];
-               if (req == NULL)
-                       continue;
-               if (i915_gem_request_completed(req))
-                       i915_gem_object_retire__read(obj, i);
-       }
-       return 0;
- }
- /**
-  * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
-  * @dev: drm device pointer
-  * @data: ioctl data blob
-  * @file: drm file pointer
-  *
-  * Returns 0 if successful, else an error is returned with the remaining time in
-  * the timeout parameter.
-  *  -ETIME: object is still busy after timeout
-  *  -ERESTARTSYS: signal interrupted the wait
-  *  -ENONENT: object doesn't exist
-  * Also possible, but rare:
-  *  -EAGAIN: GPU wedged
-  *  -ENOMEM: damn
-  *  -ENODEV: Internal IRQ fail
-  *  -E?: The add request failed
-  *
-  * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
-  * non-zero timeout parameter the wait ioctl will wait for the given number of
-  * nanoseconds on an object becoming unbusy. Since the wait itself does so
-  * without holding struct_mutex the object may become re-busied before this
-  * function completes. A similar but shorter * race condition exists in the busy
-  * ioctl
-  */
- int
- i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
- {
-       struct drm_i915_gem_wait *args = data;
-       struct drm_i915_gem_object *obj;
-       struct drm_i915_gem_request *req[I915_NUM_ENGINES];
-       int i, n = 0;
-       int ret;
-       if (args->flags != 0)
-               return -EINVAL;
-       ret = i915_mutex_lock_interruptible(dev);
-       if (ret)
-               return ret;
-       obj = to_intel_bo(drm_gem_object_lookup(file, args->bo_handle));
-       if (&obj->base == NULL) {
-               mutex_unlock(&dev->struct_mutex);
-               return -ENOENT;
-       }
-       /* Need to make sure the object gets inactive eventually. */
-       ret = i915_gem_object_flush_active(obj);
-       if (ret)
-               goto out;
-       if (!obj->active)
-               goto out;
-       /* Do this after OLR check to make sure we make forward progress polling
-        * on this IOCTL with a timeout == 0 (like busy ioctl)
-        */
-       if (args->timeout_ns == 0) {
-               ret = -ETIME;
-               goto out;
-       }
-       drm_gem_object_unreference(&obj->base);
-       for (i = 0; i < I915_NUM_ENGINES; i++) {
-               if (obj->last_read_req[i] == NULL)
-                       continue;
-               req[n++] = i915_gem_request_reference(obj->last_read_req[i]);
-       }
-       mutex_unlock(&dev->struct_mutex);
-       for (i = 0; i < n; i++) {
-               if (ret == 0)
-                       ret = __i915_wait_request(req[i], true,
-                                                 args->timeout_ns > 0 ? &args->timeout_ns : NULL,
-                                                 to_rps_client(file));
-               i915_gem_request_unreference(req[i]);
-       }
-       return ret;
- out:
-       drm_gem_object_unreference(&obj->base);
-       mutex_unlock(&dev->struct_mutex);
-       return ret;
- }
- static int
- __i915_gem_object_sync(struct drm_i915_gem_object *obj,
-                      struct intel_engine_cs *to,
-                      struct drm_i915_gem_request *from_req,
-                      struct drm_i915_gem_request **to_req)
- {
-       struct intel_engine_cs *from;
-       int ret;
-       from = i915_gem_request_get_engine(from_req);
-       if (to == from)
-               return 0;
-       if (i915_gem_request_completed(from_req))
-               return 0;
-       if (!i915_semaphore_is_enabled(to_i915(obj->base.dev))) {
-               struct drm_i915_private *i915 = to_i915(obj->base.dev);
-               ret = __i915_wait_request(from_req,
-                                         i915->mm.interruptible,
-                                         NULL,
-                                         &i915->rps.semaphores);
-               if (ret)
-                       return ret;
-               i915_gem_object_retire_request(obj, from_req);
-       } else {
-               int idx = intel_ring_sync_index(from, to);
-               u32 seqno = i915_gem_request_get_seqno(from_req);
-               WARN_ON(!to_req);
-               if (seqno <= from->semaphore.sync_seqno[idx])
-                       return 0;
-               if (*to_req == NULL) {
-                       struct drm_i915_gem_request *req;
+               goto out_unlock;
  
-                       req = i915_gem_request_alloc(to, NULL);
-                       if (IS_ERR(req))
-                               return PTR_ERR(req);
+       for_each_engine(engine, dev_priv)
+               i915_gem_batch_pool_fini(&engine->batch_pool);
  
-                       *to_req = req;
-               }
+       GEM_BUG_ON(!dev_priv->gt.awake);
+       dev_priv->gt.awake = false;
+       rearm_hangcheck = false;
  
-               trace_i915_gem_ring_sync_to(*to_req, from, from_req);
-               ret = to->semaphore.sync_to(*to_req, from, seqno);
-               if (ret)
-                       return ret;
+       if (INTEL_GEN(dev_priv) >= 6)
+               gen6_rps_idle(dev_priv);
+       intel_runtime_pm_put(dev_priv);
+ out_unlock:
+       mutex_unlock(&dev->struct_mutex);
  
-               /* We use last_read_req because sync_to()
-                * might have just caused seqno wrap under
-                * the radar.
-                */
-               from->semaphore.sync_seqno[idx] =
-                       i915_gem_request_get_seqno(obj->last_read_req[from->id]);
+ out_rearm:
+       if (rearm_hangcheck) {
+               GEM_BUG_ON(!dev_priv->gt.awake);
+               i915_queue_hangcheck(dev_priv);
        }
+ }
  
-       return 0;
+ void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
+ {
+       struct drm_i915_gem_object *obj = to_intel_bo(gem);
+       struct drm_i915_file_private *fpriv = file->driver_priv;
+       struct i915_vma *vma, *vn;
+       mutex_lock(&obj->base.dev->struct_mutex);
+       list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link)
+               if (vma->vm->file == fpriv)
+                       i915_vma_close(vma);
+       mutex_unlock(&obj->base.dev->struct_mutex);
  }
  
  /**
-  * i915_gem_object_sync - sync an object to a ring.
-  *
-  * @obj: object which may be in use on another ring.
-  * @to: ring we wish to use the object on. May be NULL.
-  * @to_req: request we wish to use the object for. See below.
-  *          This will be allocated and returned if a request is
-  *          required but not passed in.
-  *
-  * This code is meant to abstract object synchronization with the GPU.
-  * Calling with NULL implies synchronizing the object with the CPU
-  * rather than a particular GPU ring. Conceptually we serialise writes
-  * between engines inside the GPU. We only allow one engine to write
-  * into a buffer at any time, but multiple readers. To ensure each has
-  * a coherent view of memory, we must:
-  *
-  * - If there is an outstanding write request to the object, the new
-  *   request must wait for it to complete (either CPU or in hw, requests
-  *   on the same ring will be naturally ordered).
-  *
-  * - If we are a write request (pending_write_domain is set), the new
-  *   request must wait for outstanding read requests to complete.
+  * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
+  * @dev: drm device pointer
+  * @data: ioctl data blob
+  * @file: drm file pointer
   *
-  * For CPU synchronisation (NULL to) no request is required. For syncing with
-  * rings to_req must be non-NULL. However, a request does not have to be
-  * pre-allocated. If *to_req is NULL and sync commands will be emitted then a
-  * request will be allocated automatically and returned through *to_req. Note
-  * that it is not guaranteed that commands will be emitted (because the system
-  * might already be idle). Hence there is no need to create a request that
-  * might never have any work submitted. Note further that if a request is
-  * returned in *to_req, it is the responsibility of the caller to submit
-  * that request (after potentially adding more work to it).
+  * Returns 0 if successful, else an error is returned with the remaining time in
+  * the timeout parameter.
+  *  -ETIME: object is still busy after timeout
+  *  -ERESTARTSYS: signal interrupted the wait
+  *  -ENONENT: object doesn't exist
+  * Also possible, but rare:
+  *  -EAGAIN: GPU wedged
+  *  -ENOMEM: damn
+  *  -ENODEV: Internal IRQ fail
+  *  -E?: The add request failed
   *
-  * Returns 0 if successful, else propagates up the lower layer error.
+  * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
+  * non-zero timeout parameter the wait ioctl will wait for the given number of
+  * nanoseconds on an object becoming unbusy. Since the wait itself does so
+  * without holding struct_mutex the object may become re-busied before this
+  * function completes. A similar but shorter * race condition exists in the busy
+  * ioctl
   */
  int
- i915_gem_object_sync(struct drm_i915_gem_object *obj,
-                    struct intel_engine_cs *to,
-                    struct drm_i915_gem_request **to_req)
+ i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  {
-       const bool readonly = obj->base.pending_write_domain == 0;
-       struct drm_i915_gem_request *req[I915_NUM_ENGINES];
-       int ret, i, n;
+       struct drm_i915_gem_wait *args = data;
+       struct intel_rps_client *rps = to_rps_client(file);
+       struct drm_i915_gem_object *obj;
+       unsigned long active;
+       int idx, ret = 0;
  
-       if (!obj->active)
-               return 0;
+       if (args->flags != 0)
+               return -EINVAL;
  
-       if (to == NULL)
-               return i915_gem_object_wait_rendering(obj, readonly);
+       obj = i915_gem_object_lookup(file, args->bo_handle);
+       if (!obj)
+               return -ENOENT;
  
-       n = 0;
-       if (readonly) {
-               if (obj->last_write_req)
-                       req[n++] = obj->last_write_req;
-       } else {
-               for (i = 0; i < I915_NUM_ENGINES; i++)
-                       if (obj->last_read_req[i])
-                               req[n++] = obj->last_read_req[i];
-       }
-       for (i = 0; i < n; i++) {
-               ret = __i915_gem_object_sync(obj, to, req[i], to_req);
+       active = __I915_BO_ACTIVE(obj);
+       for_each_active(active, idx) {
+               s64 *timeout = args->timeout_ns >= 0 ? &args->timeout_ns : NULL;
+               ret = i915_gem_active_wait_unlocked(&obj->last_read[idx],
+                                                   I915_WAIT_INTERRUPTIBLE,
+                                                   timeout, rps);
                if (ret)
-                       return ret;
+                       break;
        }
  
-       return 0;
- }
- static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
- {
-       u32 old_write_domain, old_read_domains;
-       /* Force a pagefault for domain tracking on next user access */
-       i915_gem_release_mmap(obj);
-       if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
-               return;
-       old_read_domains = obj->base.read_domains;
-       old_write_domain = obj->base.write_domain;
-       obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
-       obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
-       trace_i915_gem_object_change_domain(obj,
-                                           old_read_domains,
-                                           old_write_domain);
+       i915_gem_object_put_unlocked(obj);
+       return ret;
  }
  
  static void __i915_vma_iounmap(struct i915_vma *vma)
  {
-       GEM_BUG_ON(vma->pin_count);
+       GEM_BUG_ON(i915_vma_is_pinned(vma));
  
        if (vma->iomap == NULL)
                return;
        vma->iomap = NULL;
  }
  
static int __i915_vma_unbind(struct i915_vma *vma, bool wait)
int i915_vma_unbind(struct i915_vma *vma)
  {
        struct drm_i915_gem_object *obj = vma->obj;
-       struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
+       unsigned long active;
        int ret;
  
-       if (list_empty(&vma->obj_link))
-               return 0;
-       if (!drm_mm_node_allocated(&vma->node)) {
-               i915_gem_vma_destroy(vma);
-               return 0;
-       }
-       if (vma->pin_count)
-               return -EBUSY;
+       /* First wait upon any activity as retiring the request may
+        * have side-effects such as unpinning or even unbinding this vma.
+        */
+       active = i915_vma_get_active(vma);
+       if (active) {
+               int idx;
+               /* When a closed VMA is retired, it is unbound - eek.
+                * In order to prevent it from being recursively closed,
+                * take a pin on the vma so that the second unbind is
+                * aborted.
+                */
+               __i915_vma_pin(vma);
  
-       BUG_ON(obj->pages == NULL);
+               for_each_active(active, idx) {
+                       ret = i915_gem_active_retire(&vma->last_read[idx],
+                                                  &vma->vm->dev->struct_mutex);
+                       if (ret)
+                               break;
+               }
  
-       if (wait) {
-               ret = i915_gem_object_wait_rendering(obj, false);
+               __i915_vma_unpin(vma);
                if (ret)
                        return ret;
+               GEM_BUG_ON(i915_vma_is_active(vma));
        }
  
-       if (vma->is_ggtt && vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
-               i915_gem_object_finish_gtt(obj);
+       if (i915_vma_is_pinned(vma))
+               return -EBUSY;
+       if (!drm_mm_node_allocated(&vma->node))
+               goto destroy;
+       GEM_BUG_ON(obj->bind_count == 0);
+       GEM_BUG_ON(!obj->pages);
  
+       if (i915_vma_is_map_and_fenceable(vma)) {
                /* release the fence reg _after_ flushing */
-               ret = i915_gem_object_put_fence(obj);
+               ret = i915_vma_put_fence(vma);
                if (ret)
                        return ret;
  
+               /* Force a pagefault for domain tracking on next user access */
+               i915_gem_release_mmap(obj);
                __i915_vma_iounmap(vma);
+               vma->flags &= ~I915_VMA_CAN_FENCE;
        }
  
-       trace_i915_vma_unbind(vma);
-       vma->vm->unbind_vma(vma);
-       vma->bound = 0;
-       list_del_init(&vma->vm_link);
-       if (vma->is_ggtt) {
-               if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
-                       obj->map_and_fenceable = false;
-               } else if (vma->ggtt_view.pages) {
-                       sg_free_table(vma->ggtt_view.pages);
-                       kfree(vma->ggtt_view.pages);
-               }
-               vma->ggtt_view.pages = NULL;
+       if (likely(!vma->vm->closed)) {
+               trace_i915_vma_unbind(vma);
+               vma->vm->unbind_vma(vma);
        }
+       vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
  
        drm_mm_remove_node(&vma->node);
-       i915_gem_vma_destroy(vma);
+       list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
+       if (vma->pages != obj->pages) {
+               GEM_BUG_ON(!vma->pages);
+               sg_free_table(vma->pages);
+               kfree(vma->pages);
+       }
+       vma->pages = NULL;
  
        /* Since the unbound list is global, only move to that list if
         * no more VMAs exist. */
-       if (list_empty(&obj->vma_list))
-               list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
+       if (--obj->bind_count == 0)
+               list_move_tail(&obj->global_list,
+                              &to_i915(obj->base.dev)->mm.unbound_list);
  
        /* And finally now the object is completely decoupled from this vma,
         * we can drop its hold on the backing storage and allow it to be
         */
        i915_gem_object_unpin_pages(obj);
  
-       return 0;
- }
- int i915_vma_unbind(struct i915_vma *vma)
- {
-       return __i915_vma_unbind(vma, true);
- }
+ destroy:
+       if (unlikely(i915_vma_is_closed(vma)))
+               i915_vma_destroy(vma);
  
- int __i915_vma_unbind_no_wait(struct i915_vma *vma)
- {
-       return __i915_vma_unbind(vma, false);
+       return 0;
  }
  
- int i915_gem_wait_for_idle(struct drm_i915_private *dev_priv)
+ int i915_gem_wait_for_idle(struct drm_i915_private *dev_priv,
+                          unsigned int flags)
  {
        struct intel_engine_cs *engine;
        int ret;
  
-       lockdep_assert_held(&dev_priv->drm.struct_mutex);
        for_each_engine(engine, dev_priv) {
                if (engine->last_context == NULL)
                        continue;
  
-               ret = intel_engine_idle(engine);
+               ret = intel_engine_idle(engine, flags);
                if (ret)
                        return ret;
        }
  
-       WARN_ON(i915_verify_lists(dev));
        return 0;
  }
  
@@@ -3759,128 -2979,87 +2979,87 @@@ static bool i915_gem_valid_gtt_space(st
  }
  
  /**
-  * Finds free space in the GTT aperture and binds the object or a view of it
-  * there.
-  * @obj: object to bind
-  * @vm: address space to bind into
-  * @ggtt_view: global gtt view if applicable
-  * @alignment: requested alignment
+  * i915_vma_insert - finds a slot for the vma in its address space
+  * @vma: the vma
+  * @size: requested size in bytes (can be larger than the VMA)
+  * @alignment: required alignment
   * @flags: mask of PIN_* flags to use
+  *
+  * First we try to allocate some free space that meets the requirements for
+  * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
+  * preferrably the oldest idle entry to make room for the new VMA.
+  *
+  * Returns:
+  * 0 on success, negative error code otherwise.
   */
- static struct i915_vma *
- i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
-                          struct i915_address_space *vm,
-                          const struct i915_ggtt_view *ggtt_view,
-                          unsigned alignment,
-                          uint64_t flags)
+ static int
+ i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
  {
-       struct drm_device *dev = obj->base.dev;
-       struct drm_i915_private *dev_priv = to_i915(dev);
-       struct i915_ggtt *ggtt = &dev_priv->ggtt;
-       u32 fence_alignment, unfenced_alignment;
-       u32 search_flag, alloc_flag;
+       struct drm_i915_private *dev_priv = to_i915(vma->vm->dev);
+       struct drm_i915_gem_object *obj = vma->obj;
        u64 start, end;
-       u64 size, fence_size;
-       struct i915_vma *vma;
        int ret;
  
-       if (i915_is_ggtt(vm)) {
-               u32 view_size;
-               if (WARN_ON(!ggtt_view))
-                       return ERR_PTR(-EINVAL);
-               view_size = i915_ggtt_view_size(obj, ggtt_view);
-               fence_size = i915_gem_get_gtt_size(dev,
-                                                  view_size,
-                                                  obj->tiling_mode);
-               fence_alignment = i915_gem_get_gtt_alignment(dev,
-                                                            view_size,
-                                                            obj->tiling_mode,
-                                                            true);
-               unfenced_alignment = i915_gem_get_gtt_alignment(dev,
-                                                               view_size,
-                                                               obj->tiling_mode,
-                                                               false);
-               size = flags & PIN_MAPPABLE ? fence_size : view_size;
-       } else {
-               fence_size = i915_gem_get_gtt_size(dev,
-                                                  obj->base.size,
-                                                  obj->tiling_mode);
-               fence_alignment = i915_gem_get_gtt_alignment(dev,
-                                                            obj->base.size,
-                                                            obj->tiling_mode,
-                                                            true);
-               unfenced_alignment =
-                       i915_gem_get_gtt_alignment(dev,
-                                                  obj->base.size,
-                                                  obj->tiling_mode,
-                                                  false);
-               size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
-       }
+       GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
+       GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
+       size = max(size, vma->size);
+       if (flags & PIN_MAPPABLE)
+               size = i915_gem_get_ggtt_size(dev_priv, size,
+                                             i915_gem_object_get_tiling(obj));
+       alignment = max(max(alignment, vma->display_alignment),
+                       i915_gem_get_ggtt_alignment(dev_priv, size,
+                                                   i915_gem_object_get_tiling(obj),
+                                                   flags & PIN_MAPPABLE));
  
        start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
-       end = vm->total;
+       end = vma->vm->total;
        if (flags & PIN_MAPPABLE)
-               end = min_t(u64, end, ggtt->mappable_end);
+               end = min_t(u64, end, dev_priv->ggtt.mappable_end);
        if (flags & PIN_ZONE_4G)
                end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE);
  
-       if (alignment == 0)
-               alignment = flags & PIN_MAPPABLE ? fence_alignment :
-                                               unfenced_alignment;
-       if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
-               DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
-                         ggtt_view ? ggtt_view->type : 0,
-                         alignment);
-               return ERR_PTR(-EINVAL);
-       }
        /* If binding the object/GGTT view requires more space than the entire
         * aperture has, reject it early before evicting everything in a vain
         * attempt to find space.
         */
        if (size > end) {
-               DRM_DEBUG("Attempting to bind an object (view type=%u) larger than the aperture: size=%llu > %s aperture=%llu\n",
-                         ggtt_view ? ggtt_view->type : 0,
-                         size,
+               DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu [object=%zd] > %s aperture=%llu\n",
+                         size, obj->base.size,
                          flags & PIN_MAPPABLE ? "mappable" : "total",
                          end);
-               return ERR_PTR(-E2BIG);
+               return -E2BIG;
        }
  
        ret = i915_gem_object_get_pages(obj);
        if (ret)
-               return ERR_PTR(ret);
+               return ret;
  
        i915_gem_object_pin_pages(obj);
  
-       vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
-                         i915_gem_obj_lookup_or_create_vma(obj, vm);
-       if (IS_ERR(vma))
-               goto err_unpin;
        if (flags & PIN_OFFSET_FIXED) {
-               uint64_t offset = flags & PIN_OFFSET_MASK;
-               if (offset & (alignment - 1) || offset + size > end) {
+               u64 offset = flags & PIN_OFFSET_MASK;
+               if (offset & (alignment - 1) || offset > end - size) {
                        ret = -EINVAL;
-                       goto err_free_vma;
+                       goto err_unpin;
                }
                vma->node.start = offset;
                vma->node.size = size;
                vma->node.color = obj->cache_level;
-               ret = drm_mm_reserve_node(&vm->mm, &vma->node);
+               ret = drm_mm_reserve_node(&vma->vm->mm, &vma->node);
                if (ret) {
                        ret = i915_gem_evict_for_vma(vma);
                        if (ret == 0)
-                               ret = drm_mm_reserve_node(&vm->mm, &vma->node);
+                               ret = drm_mm_reserve_node(&vma->vm->mm, &vma->node);
+                       if (ret)
+                               goto err_unpin;
                }
-               if (ret)
-                       goto err_free_vma;
        } else {
+               u32 search_flag, alloc_flag;
                if (flags & PIN_HIGH) {
                        search_flag = DRM_MM_SEARCH_BELOW;
                        alloc_flag = DRM_MM_CREATE_TOP;
                        alloc_flag = DRM_MM_CREATE_DEFAULT;
                }
  
+               /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
+                * so we know that we always have a minimum alignment of 4096.
+                * The drm_mm range manager is optimised to return results
+                * with zero alignment, so where possible use the optimal
+                * path.
+                */
+               if (alignment <= 4096)
+                       alignment = 0;
  search_free:
-               ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
+               ret = drm_mm_insert_node_in_range_generic(&vma->vm->mm,
+                                                         &vma->node,
                                                          size, alignment,
                                                          obj->cache_level,
                                                          start, end,
                                                          search_flag,
                                                          alloc_flag);
                if (ret) {
-                       ret = i915_gem_evict_something(dev, vm, size, alignment,
+                       ret = i915_gem_evict_something(vma->vm, size, alignment,
                                                       obj->cache_level,
                                                       start, end,
                                                       flags);
                        if (ret == 0)
                                goto search_free;
  
-                       goto err_free_vma;
+                       goto err_unpin;
                }
        }
-       if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
-               ret = -EINVAL;
-               goto err_remove_node;
-       }
-       trace_i915_vma_bind(vma, flags);
-       ret = i915_vma_bind(vma, obj->cache_level, flags);
-       if (ret)
-               goto err_remove_node;
+       GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level));
  
        list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
-       list_add_tail(&vma->vm_link, &vm->inactive_list);
+       list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
+       obj->bind_count++;
  
-       return vma;
+       return 0;
  
- err_remove_node:
-       drm_mm_remove_node(&vma->node);
- err_free_vma:
-       i915_gem_vma_destroy(vma);
-       vma = ERR_PTR(ret);
  err_unpin:
        i915_gem_object_unpin_pages(obj);
-       return vma;
+       return ret;
  }
  
  bool
@@@ -3974,51 -3151,72 +3151,72 @@@ i915_gem_clflush_object(struct drm_i915
  static void
  i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  {
-       uint32_t old_write_domain;
+       struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  
        if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
                return;
  
        /* No actual flushing is required for the GTT write domain.  Writes
-        * to it immediately go to main memory as far as we know, so there's
+        * to it "immediately" go to main memory as far as we know, so there's
         * no chipset flush.  It also doesn't land in render cache.
         *
         * However, we do have to enforce the order so that all writes through
         * the GTT land before any writes to the device, such as updates to
         * the GATT itself.
+        *
+        * We also have to wait a bit for the writes to land from the GTT.
+        * An uncached read (i.e. mmio) seems to be ideal for the round-trip
+        * timing. This issue has only been observed when switching quickly
+        * between GTT writes and CPU reads from inside the kernel on recent hw,
+        * and it appears to only affect discrete GTT blocks (i.e. on LLC
+        * system agents we cannot reproduce this behaviour).
         */
        wmb();
+       if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv))
+               POSTING_READ(RING_ACTHD(dev_priv->engine[RCS].mmio_base));
  
-       old_write_domain = obj->base.write_domain;
-       obj->base.write_domain = 0;
-       intel_fb_obj_flush(obj, false, ORIGIN_GTT);
+       intel_fb_obj_flush(obj, false, write_origin(obj, I915_GEM_DOMAIN_GTT));
  
+       obj->base.write_domain = 0;
        trace_i915_gem_object_change_domain(obj,
                                            obj->base.read_domains,
-                                           old_write_domain);
+                                           I915_GEM_DOMAIN_GTT);
  }
  
  /** Flushes the CPU write domain for the object if it's dirty. */
  static void
  i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  {
-       uint32_t old_write_domain;
        if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
                return;
  
        if (i915_gem_clflush_object(obj, obj->pin_display))
                i915_gem_chipset_flush(to_i915(obj->base.dev));
  
-       old_write_domain = obj->base.write_domain;
-       obj->base.write_domain = 0;
        intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  
+       obj->base.write_domain = 0;
        trace_i915_gem_object_change_domain(obj,
                                            obj->base.read_domains,
-                                           old_write_domain);
+                                           I915_GEM_DOMAIN_CPU);
+ }
+ static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
+ {
+       struct i915_vma *vma;
+       list_for_each_entry(vma, &obj->vma_list, obj_link) {
+               if (!i915_vma_is_ggtt(vma))
+                       continue;
+               if (i915_vma_is_active(vma))
+                       continue;
+               if (!drm_mm_node_allocated(&vma->node))
+                       continue;
+               list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
+       }
  }
  
  /**
  int
  i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  {
-       struct drm_device *dev = obj->base.dev;
-       struct drm_i915_private *dev_priv = to_i915(dev);
-       struct i915_ggtt *ggtt = &dev_priv->ggtt;
        uint32_t old_write_domain, old_read_domains;
-       struct i915_vma *vma;
        int ret;
  
-       if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
-               return 0;
        ret = i915_gem_object_wait_rendering(obj, !write);
        if (ret)
                return ret;
  
+       if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
+               return 0;
        /* Flush and acquire obj->pages so that we are coherent through
         * direct access in memory with previous cached writes through
         * shmemfs and that our cache domain tracking remains valid.
                                            old_write_domain);
  
        /* And bump the LRU for this access */
-       vma = i915_gem_obj_to_ggtt(obj);
-       if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
-               list_move_tail(&vma->vm_link,
-                              &ggtt->base.inactive_list);
+       i915_gem_object_bump_inactive_ggtt(obj);
  
        return 0;
  }
  int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
                                    enum i915_cache_level cache_level)
  {
-       struct drm_device *dev = obj->base.dev;
-       struct i915_vma *vma, *next;
-       bool bound = false;
+       struct i915_vma *vma;
        int ret = 0;
  
        if (obj->cache_level == cache_level)
         * catch the issue of the CS prefetch crossing page boundaries and
         * reading an invalid PTE on older architectures.
         */
-       list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
+ restart:
+       list_for_each_entry(vma, &obj->vma_list, obj_link) {
                if (!drm_mm_node_allocated(&vma->node))
                        continue;
  
-               if (vma->pin_count) {
+               if (i915_vma_is_pinned(vma)) {
                        DRM_DEBUG("can not change the cache level of pinned objects\n");
                        return -EBUSY;
                }
  
-               if (!i915_gem_valid_gtt_space(vma, cache_level)) {
-                       ret = i915_vma_unbind(vma);
-                       if (ret)
-                               return ret;
-               } else
-                       bound = true;
+               if (i915_gem_valid_gtt_space(vma, cache_level))
+                       continue;
+               ret = i915_vma_unbind(vma);
+               if (ret)
+                       return ret;
+               /* As unbinding may affect other elements in the
+                * obj->vma_list (due to side-effects from retiring
+                * an active vma), play safe and restart the iterator.
+                */
+               goto restart;
        }
  
        /* We can reuse the existing drm_mm nodes but need to change the
         * rewrite the PTE in the belief that doing so tramples upon less
         * state and so involves less work.
         */
-       if (bound) {
+       if (obj->bind_count) {
                /* Before we change the PTE, the GPU must not be accessing it.
                 * If we wait upon the object, we know that all the bound
                 * VMA are no longer active.
                if (ret)
                        return ret;
  
-               if (!HAS_LLC(dev) && cache_level != I915_CACHE_NONE) {
+               if (!HAS_LLC(obj->base.dev) && cache_level != I915_CACHE_NONE) {
                        /* Access to snoopable pages through the GTT is
                         * incoherent and on some machines causes a hard
                         * lockup. Relinquish the CPU mmaping to force
                         * dropped the fence as all snoopable access is
                         * supposed to be linear.
                         */
-                       ret = i915_gem_object_put_fence(obj);
-                       if (ret)
-                               return ret;
+                       list_for_each_entry(vma, &obj->vma_list, obj_link) {
+                               ret = i915_vma_put_fence(vma);
+                               if (ret)
+                                       return ret;
+                       }
                } else {
                        /* We either have incoherent backing store and
                         * so no GTT access or the architecture is fully
@@@ -4221,8 -3419,8 +3419,8 @@@ int i915_gem_get_caching_ioctl(struct d
        struct drm_i915_gem_caching *args = data;
        struct drm_i915_gem_object *obj;
  
-       obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
-       if (&obj->base == NULL)
+       obj = i915_gem_object_lookup(file, args->handle);
+       if (!obj)
                return -ENOENT;
  
        switch (obj->cache_level) {
                break;
        }
  
-       drm_gem_object_unreference_unlocked(&obj->base);
+       i915_gem_object_put_unlocked(obj);
        return 0;
  }
  
@@@ -4282,15 -3480,15 +3480,15 @@@ int i915_gem_set_caching_ioctl(struct d
        if (ret)
                goto rpm_put;
  
-       obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
-       if (&obj->base == NULL) {
+       obj = i915_gem_object_lookup(file, args->handle);
+       if (!obj) {
                ret = -ENOENT;
                goto unlock;
        }
  
        ret = i915_gem_object_set_cache_level(obj, level);
  
-       drm_gem_object_unreference(&obj->base);
+       i915_gem_object_put(obj);
  unlock:
        mutex_unlock(&dev->struct_mutex);
  rpm_put:
   * Can be called from an uninterruptible phase (modesetting) and allows
   * any flushes to be pipelined (for pageflips).
   */
- int
+ struct i915_vma *
  i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
                                     u32 alignment,
                                     const struct i915_ggtt_view *view)
  {
+       struct i915_vma *vma;
        u32 old_read_domains, old_write_domain;
        int ret;
  
         */
        ret = i915_gem_object_set_cache_level(obj,
                                              HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
-       if (ret)
+       if (ret) {
+               vma = ERR_PTR(ret);
                goto err_unpin_display;
+       }
  
        /* As the user may map the buffer once pinned in the display plane
         * (e.g. libkms for the bootup splash), we have to ensure that we
-        * always use map_and_fenceable for all scanout buffers.
+        * always use map_and_fenceable for all scanout buffers. However,
+        * it may simply be too big to fit into mappable, in which case
+        * put it anyway and hope that userspace can cope (but always first
+        * try to preserve the existing ABI).
         */
-       ret = i915_gem_object_ggtt_pin(obj, view, alignment,
-                                      view->type == I915_GGTT_VIEW_NORMAL ?
-                                      PIN_MAPPABLE : 0);
-       if (ret)
+       vma = ERR_PTR(-ENOSPC);
+       if (view->type == I915_GGTT_VIEW_NORMAL)
+               vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
+                                              PIN_MAPPABLE | PIN_NONBLOCK);
+       if (IS_ERR(vma))
+               vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, 0);
+       if (IS_ERR(vma))
                goto err_unpin_display;
  
+       vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
+       WARN_ON(obj->pin_display > i915_vma_pin_count(vma));
        i915_gem_object_flush_cpu_write_domain(obj);
  
        old_write_domain = obj->base.write_domain;
                                            old_read_domains,
                                            old_write_domain);
  
-       return 0;
+       return vma;
  
  err_unpin_display:
        obj->pin_display--;
-       return ret;
+       return vma;
  }
  
  void
- i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
-                                        const struct i915_ggtt_view *view)
+ i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
  {
-       if (WARN_ON(obj->pin_display == 0))
+       if (WARN_ON(vma->obj->pin_display == 0))
                return;
  
-       i915_gem_object_ggtt_unpin_view(obj, view);
+       if (--vma->obj->pin_display == 0)
+               vma->display_alignment = 0;
  
-       obj->pin_display--;
+       /* Bump the LRU to try and avoid premature eviction whilst flipping  */
+       if (!i915_vma_is_active(vma))
+               list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
+       i915_vma_unpin(vma);
+       WARN_ON(vma->obj->pin_display > i915_vma_pin_count(vma));
  }
  
  /**
@@@ -4389,13 -3605,13 +3605,13 @@@ i915_gem_object_set_to_cpu_domain(struc
        uint32_t old_write_domain, old_read_domains;
        int ret;
  
-       if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
-               return 0;
        ret = i915_gem_object_wait_rendering(obj, !write);
        if (ret)
                return ret;
  
+       if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
+               return 0;
        i915_gem_object_flush_gtt_write_domain(obj);
  
        old_write_domain = obj->base.write_domain;
@@@ -4470,28 -3686,31 +3686,31 @@@ i915_gem_ring_throttle(struct drm_devic
                target = request;
        }
        if (target)
-               i915_gem_request_reference(target);
+               i915_gem_request_get(target);
        spin_unlock(&file_priv->mm.lock);
  
        if (target == NULL)
                return 0;
  
-       ret = __i915_wait_request(target, true, NULL, NULL);
-       i915_gem_request_unreference(target);
+       ret = i915_wait_request(target, I915_WAIT_INTERRUPTIBLE, NULL, NULL);
+       i915_gem_request_put(target);
  
        return ret;
  }
  
  static bool
- i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
+ i915_vma_misplaced(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
  {
-       struct drm_i915_gem_object *obj = vma->obj;
+       if (!drm_mm_node_allocated(&vma->node))
+               return false;
  
-       if (alignment &&
-           vma->node.start & (alignment - 1))
+       if (vma->node.size < size)
                return true;
  
-       if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
+       if (alignment && vma->node.start & (alignment - 1))
+               return true;
+       if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
                return true;
  
        if (flags & PIN_OFFSET_BIAS &&
  void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
  {
        struct drm_i915_gem_object *obj = vma->obj;
+       struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
        bool mappable, fenceable;
        u32 fence_size, fence_alignment;
  
-       fence_size = i915_gem_get_gtt_size(obj->base.dev,
-                                          obj->base.size,
-                                          obj->tiling_mode);
-       fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
-                                                    obj->base.size,
-                                                    obj->tiling_mode,
-                                                    true);
+       fence_size = i915_gem_get_ggtt_size(dev_priv,
+                                           vma->size,
+                                           i915_gem_object_get_tiling(obj));
+       fence_alignment = i915_gem_get_ggtt_alignment(dev_priv,
+                                                     vma->size,
+                                                     i915_gem_object_get_tiling(obj),
+                                                     true);
  
        fenceable = (vma->node.size == fence_size &&
                     (vma->node.start & (fence_alignment - 1)) == 0);
  
        mappable = (vma->node.start + fence_size <=
-                   to_i915(obj->base.dev)->ggtt.mappable_end);
+                   dev_priv->ggtt.mappable_end);
  
-       obj->map_and_fenceable = mappable && fenceable;
+       if (mappable && fenceable)
+               vma->flags |= I915_VMA_CAN_FENCE;
+       else
+               vma->flags &= ~I915_VMA_CAN_FENCE;
  }
  
- static int
- i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
-                      struct i915_address_space *vm,
-                      const struct i915_ggtt_view *ggtt_view,
-                      uint32_t alignment,
-                      uint64_t flags)
+ int __i915_vma_do_pin(struct i915_vma *vma,
+                     u64 size, u64 alignment, u64 flags)
  {
-       struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
-       struct i915_vma *vma;
-       unsigned bound;
+       unsigned int bound = vma->flags;
        int ret;
  
-       if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
-               return -ENODEV;
-       if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
-               return -EINVAL;
+       GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
+       GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
  
-       if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
-               return -EINVAL;
+       if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
+               ret = -EBUSY;
+               goto err;
+       }
  
-       if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
-               return -EINVAL;
+       if ((bound & I915_VMA_BIND_MASK) == 0) {
+               ret = i915_vma_insert(vma, size, alignment, flags);
+               if (ret)
+                       goto err;
+       }
  
-       vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
-                         i915_gem_obj_to_vma(obj, vm);
+       ret = i915_vma_bind(vma, vma->obj->cache_level, flags);
+       if (ret)
+               goto err;
  
-       if (vma) {
-               if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
-                       return -EBUSY;
+       if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
+               __i915_vma_set_map_and_fenceable(vma);
  
-               if (i915_vma_misplaced(vma, alignment, flags)) {
-                       WARN(vma->pin_count,
-                            "bo is already pinned in %s with incorrect alignment:"
-                            " offset=%08x %08x, req.alignment=%x, req.map_and_fenceable=%d,"
-                            " obj->map_and_fenceable=%d\n",
-                            ggtt_view ? "ggtt" : "ppgtt",
-                            upper_32_bits(vma->node.start),
-                            lower_32_bits(vma->node.start),
-                            alignment,
-                            !!(flags & PIN_MAPPABLE),
-                            obj->map_and_fenceable);
-                       ret = i915_vma_unbind(vma);
-                       if (ret)
-                               return ret;
+       GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
+       return 0;
  
-                       vma = NULL;
-               }
-       }
+ err:
+       __i915_vma_unpin(vma);
+       return ret;
+ }
  
-       bound = vma ? vma->bound : 0;
-       if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
-               vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
-                                                flags);
-               if (IS_ERR(vma))
-                       return PTR_ERR(vma);
-       } else {
-               ret = i915_vma_bind(vma, obj->cache_level, flags);
+ struct i915_vma *
+ i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
+                        const struct i915_ggtt_view *view,
+                        u64 size,
+                        u64 alignment,
+                        u64 flags)
+ {
+       struct i915_address_space *vm = &to_i915(obj->base.dev)->ggtt.base;
+       struct i915_vma *vma;
+       int ret;
+       vma = i915_gem_obj_lookup_or_create_vma(obj, vm, view);
+       if (IS_ERR(vma))
+               return vma;
+       if (i915_vma_misplaced(vma, size, alignment, flags)) {
+               if (flags & PIN_NONBLOCK &&
+                   (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)))
+                       return ERR_PTR(-ENOSPC);
+               WARN(i915_vma_is_pinned(vma),
+                    "bo is already pinned in ggtt with incorrect alignment:"
+                    " offset=%08x, req.alignment=%llx,"
+                    " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
+                    i915_ggtt_offset(vma), alignment,
+                    !!(flags & PIN_MAPPABLE),
+                    i915_vma_is_map_and_fenceable(vma));
+               ret = i915_vma_unbind(vma);
                if (ret)
-                       return ret;
+                       return ERR_PTR(ret);
        }
  
-       if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
-           (bound ^ vma->bound) & GLOBAL_BIND) {
-               __i915_vma_set_map_and_fenceable(vma);
-               WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
-       }
+       ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
+       if (ret)
+               return ERR_PTR(ret);
  
-       vma->pin_count++;
-       return 0;
+       return vma;
  }
  
- int
- i915_gem_object_pin(struct drm_i915_gem_object *obj,
-                   struct i915_address_space *vm,
-                   uint32_t alignment,
-                   uint64_t flags)
+ static __always_inline unsigned int __busy_read_flag(unsigned int id)
  {
-       return i915_gem_object_do_pin(obj, vm,
-                                     i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
-                                     alignment, flags);
+       /* Note that we could alias engines in the execbuf API, but
+        * that would be very unwise as it prevents userspace from
+        * fine control over engine selection. Ahem.
+        *
+        * This should be something like EXEC_MAX_ENGINE instead of
+        * I915_NUM_ENGINES.
+        */
+       BUILD_BUG_ON(I915_NUM_ENGINES > 16);
+       return 0x10000 << id;
  }
  
- int
- i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
-                        const struct i915_ggtt_view *view,
-                        uint32_t alignment,
-                        uint64_t flags)
+ static __always_inline unsigned int __busy_write_id(unsigned int id)
  {
-       struct drm_device *dev = obj->base.dev;
-       struct drm_i915_private *dev_priv = to_i915(dev);
-       struct i915_ggtt *ggtt = &dev_priv->ggtt;
+       /* The uABI guarantees an active writer is also amongst the read
+        * engines. This would be true if we accessed the activity tracking
+        * under the lock, but as we perform the lookup of the object and
+        * its activity locklessly we can not guarantee that the last_write
+        * being active implies that we have set the same engine flag from
+        * last_read - hence we always set both read and write busy for
+        * last_write.
+        */
+       return id | __busy_read_flag(id);
+ }
+ static __always_inline unsigned int
+ __busy_set_if_active(const struct i915_gem_active *active,
+                    unsigned int (*flag)(unsigned int id))
+ {
+       struct drm_i915_gem_request *request;
  
-       BUG_ON(!view);
+       request = rcu_dereference(active->request);
+       if (!request || i915_gem_request_completed(request))
+               return 0;
  
-       return i915_gem_object_do_pin(obj, &ggtt->base, view,
-                                     alignment, flags | PIN_GLOBAL);
+       /* This is racy. See __i915_gem_active_get_rcu() for an in detail
+        * discussion of how to handle the race correctly, but for reporting
+        * the busy state we err on the side of potentially reporting the
+        * wrong engine as being busy (but we guarantee that the result
+        * is at least self-consistent).
+        *
+        * As we use SLAB_DESTROY_BY_RCU, the request may be reallocated
+        * whilst we are inspecting it, even under the RCU read lock as we are.
+        * This means that there is a small window for the engine and/or the
+        * seqno to have been overwritten. The seqno will always be in the
+        * future compared to the intended, and so we know that if that
+        * seqno is idle (on whatever engine) our request is idle and the
+        * return 0 above is correct.
+        *
+        * The issue is that if the engine is switched, it is just as likely
+        * to report that it is busy (but since the switch happened, we know
+        * the request should be idle). So there is a small chance that a busy
+        * result is actually the wrong engine.
+        *
+        * So why don't we care?
+        *
+        * For starters, the busy ioctl is a heuristic that is by definition
+        * racy. Even with perfect serialisation in the driver, the hardware
+        * state is constantly advancing - the state we report to the user
+        * is stale.
+        *
+        * The critical information for the busy-ioctl is whether the object
+        * is idle as userspace relies on that to detect whether its next
+        * access will stall, or if it has missed submitting commands to
+        * the hardware allowing the GPU to stall. We never generate a
+        * false-positive for idleness, thus busy-ioctl is reliable at the
+        * most fundamental level, and we maintain the guarantee that a
+        * busy object left to itself will eventually become idle (and stay
+        * idle!).
+        *
+        * We allow ourselves the leeway of potentially misreporting the busy
+        * state because that is an optimisation heuristic that is constantly
+        * in flux. Being quickly able to detect the busy/idle state is much
+        * more important than accurate logging of exactly which engines were
+        * busy.
+        *
+        * For accuracy in reporting the engine, we could use
+        *
+        *      result = 0;
+        *      request = __i915_gem_active_get_rcu(active);
+        *      if (request) {
+        *              if (!i915_gem_request_completed(request))
+        *                      result = flag(request->engine->exec_id);
+        *              i915_gem_request_put(request);
+        *      }
+        *
+        * but that still remains susceptible to both hardware and userspace
+        * races. So we accept making the result of that race slightly worse,
+        * given the rarity of the race and its low impact on the result.
+        */
+       return flag(READ_ONCE(request->engine->exec_id));
  }
  
- void
- i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
-                               const struct i915_ggtt_view *view)
+ static __always_inline unsigned int
+ busy_check_reader(const struct i915_gem_active *active)
  {
-       struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
-       WARN_ON(vma->pin_count == 0);
-       WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
+       return __busy_set_if_active(active, __busy_read_flag);
+ }
  
-       --vma->pin_count;
+ static __always_inline unsigned int
+ busy_check_writer(const struct i915_gem_active *active)
+ {
+       return __busy_set_if_active(active, __busy_write_id);
  }
  
  int
@@@ -4645,47 -3937,64 +3937,64 @@@ i915_gem_busy_ioctl(struct drm_device *
  {
        struct drm_i915_gem_busy *args = data;
        struct drm_i915_gem_object *obj;
-       int ret;
+       unsigned long active;
  
-       ret = i915_mutex_lock_interruptible(dev);
-       if (ret)
-               return ret;
+       obj = i915_gem_object_lookup(file, args->handle);
+       if (!obj)
+               return -ENOENT;
  
-       obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
-       if (&obj->base == NULL) {
-               ret = -ENOENT;
-               goto unlock;
-       }
+       args->busy = 0;
+       active = __I915_BO_ACTIVE(obj);
+       if (active) {
+               int idx;
  
-       /* Count all active objects as busy, even if they are currently not used
-        * by the gpu. Users of this interface expect objects to eventually
-        * become non-busy without any further actions, therefore emit any
-        * necessary flushes here.
-        */
-       ret = i915_gem_object_flush_active(obj);
-       if (ret)
-               goto unref;
+               /* Yes, the lookups are intentionally racy.
+                *
+                * First, we cannot simply rely on __I915_BO_ACTIVE. We have
+                * to regard the value as stale and as our ABI guarantees
+                * forward progress, we confirm the status of each active
+                * request with the hardware.
+                *
+                * Even though we guard the pointer lookup by RCU, that only
+                * guarantees that the pointer and its contents remain
+                * dereferencable and does *not* mean that the request we
+                * have is the same as the one being tracked by the object.
+                *
+                * Consider that we lookup the request just as it is being
+                * retired and freed. We take a local copy of the pointer,
+                * but before we add its engine into the busy set, the other
+                * thread reallocates it and assigns it to a task on another
+                * engine with a fresh and incomplete seqno. Guarding against
+                * that requires careful serialisation and reference counting,
+                * i.e. using __i915_gem_active_get_request_rcu(). We don't,
+                * instead we expect that if the result is busy, which engines
+                * are busy is not completely reliable - we only guarantee
+                * that the object was busy.
+                */
+               rcu_read_lock();
  
-       args->busy = 0;
-       if (obj->active) {
-               int i;
+               for_each_active(active, idx)
+                       args->busy |= busy_check_reader(&obj->last_read[idx]);
  
-               for (i = 0; i < I915_NUM_ENGINES; i++) {
-                       struct drm_i915_gem_request *req;
+               /* For ABI sanity, we only care that the write engine is in
+                * the set of read engines. This should be ensured by the
+                * ordering of setting last_read/last_write in
+                * i915_vma_move_to_active(), and then in reverse in retire.
+                * However, for good measure, we always report the last_write
+                * request as a busy read as well as being a busy write.
+                *
+                * We don't care that the set of active read/write engines
+                * may change during construction of the result, as it is
+                * equally liable to change before userspace can inspect
+                * the result.
+                */
+               args->busy |= busy_check_writer(&obj->last_write);
  
-                       req = obj->last_read_req[i];
-                       if (req)
-                               args->busy |= 1 << (16 + req->engine->exec_id);
-               }
-               if (obj->last_write_req)
-                       args->busy |= obj->last_write_req->engine->exec_id;
+               rcu_read_unlock();
        }
  
- unref:
-       drm_gem_object_unreference(&obj->base);
- unlock:
-       mutex_unlock(&dev->struct_mutex);
-       return ret;
+       i915_gem_object_put_unlocked(obj);
+       return 0;
  }
  
  int
@@@ -4716,19 -4025,14 +4025,14 @@@ i915_gem_madvise_ioctl(struct drm_devic
        if (ret)
                return ret;
  
-       obj = to_intel_bo(drm_gem_object_lookup(file_priv, args->handle));
-       if (&obj->base == NULL) {
+       obj = i915_gem_object_lookup(file_priv, args->handle);
+       if (!obj) {
                ret = -ENOENT;
                goto unlock;
        }
  
-       if (i915_gem_obj_is_pinned(obj)) {
-               ret = -EINVAL;
-               goto out;
-       }
        if (obj->pages &&
-           obj->tiling_mode != I915_TILING_NONE &&
+           i915_gem_object_is_tiled(obj) &&
            dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
                if (obj->madv == I915_MADV_WILLNEED)
                        i915_gem_object_unpin_pages(obj);
  
        args->retained = obj->madv != __I915_MADV_PURGED;
  
- out:
-       drm_gem_object_unreference(&obj->base);
+       i915_gem_object_put(obj);
  unlock:
        mutex_unlock(&dev->struct_mutex);
        return ret;
@@@ -4759,14 -4062,17 +4062,17 @@@ void i915_gem_object_init(struct drm_i9
  
        INIT_LIST_HEAD(&obj->global_list);
        for (i = 0; i < I915_NUM_ENGINES; i++)
-               INIT_LIST_HEAD(&obj->engine_list[i]);
+               init_request_active(&obj->last_read[i],
+                                   i915_gem_object_retire__read);
+       init_request_active(&obj->last_write,
+                           i915_gem_object_retire__write);
        INIT_LIST_HEAD(&obj->obj_exec_link);
        INIT_LIST_HEAD(&obj->vma_list);
        INIT_LIST_HEAD(&obj->batch_pool_link);
  
        obj->ops = ops;
  
-       obj->fence_reg = I915_FENCE_REG_NONE;
+       obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
        obj->madv = I915_MADV_WILLNEED;
  
        i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
@@@ -4871,33 -4177,31 +4177,31 @@@ void i915_gem_free_object(struct drm_ge
  
        trace_i915_gem_object_destroy(obj);
  
+       /* All file-owned VMA should have been released by this point through
+        * i915_gem_close_object(), or earlier by i915_gem_context_close().
+        * However, the object may also be bound into the global GTT (e.g.
+        * older GPUs without per-process support, or for direct access through
+        * the GTT either for the user or for scanout). Those VMA still need to
+        * unbound now.
+        */
        list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
-               int ret;
-               vma->pin_count = 0;
-               ret = i915_vma_unbind(vma);
-               if (WARN_ON(ret == -ERESTARTSYS)) {
-                       bool was_interruptible;
-                       was_interruptible = dev_priv->mm.interruptible;
-                       dev_priv->mm.interruptible = false;
-                       WARN_ON(i915_vma_unbind(vma));
-                       dev_priv->mm.interruptible = was_interruptible;
-               }
+               GEM_BUG_ON(!i915_vma_is_ggtt(vma));
+               GEM_BUG_ON(i915_vma_is_active(vma));
+               vma->flags &= ~I915_VMA_PIN_MASK;
+               i915_vma_close(vma);
        }
+       GEM_BUG_ON(obj->bind_count);
  
        /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
         * before progressing. */
        if (obj->stolen)
                i915_gem_object_unpin_pages(obj);
  
-       WARN_ON(obj->frontbuffer_bits);
+       WARN_ON(atomic_read(&obj->frontbuffer_bits));
  
        if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
            dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
-           obj->tiling_mode != I915_TILING_NONE)
+           i915_gem_object_is_tiled(obj))
                i915_gem_object_unpin_pages(obj);
  
        if (WARN_ON(obj->pages_pin_count))
        if (discard_backing_storage(obj))
                obj->madv = I915_MADV_DONTNEED;
        i915_gem_object_put_pages(obj);
-       i915_gem_object_free_mmap_offset(obj);
  
        BUG_ON(obj->pages);
  
        intel_runtime_pm_put(dev_priv);
  }
  
- struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
-                                    struct i915_address_space *vm)
- {
-       struct i915_vma *vma;
-       list_for_each_entry(vma, &obj->vma_list, obj_link) {
-               if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL &&
-                   vma->vm == vm)
-                       return vma;
-       }
-       return NULL;
- }
- struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
-                                          const struct i915_ggtt_view *view)
- {
-       struct i915_vma *vma;
-       GEM_BUG_ON(!view);
-       list_for_each_entry(vma, &obj->vma_list, obj_link)
-               if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
-                       return vma;
-       return NULL;
- }
- void i915_gem_vma_destroy(struct i915_vma *vma)
- {
-       WARN_ON(vma->node.allocated);
-       /* Keep the vma as a placeholder in the execbuffer reservation lists */
-       if (!list_empty(&vma->exec_list))
-               return;
-       if (!vma->is_ggtt)
-               i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
-       list_del(&vma->obj_link);
-       kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
- }
- static void
- i915_gem_stop_engines(struct drm_device *dev)
+ int i915_gem_suspend(struct drm_device *dev)
  {
        struct drm_i915_private *dev_priv = to_i915(dev);
-       struct intel_engine_cs *engine;
-       for_each_engine(engine, dev_priv)
-               dev_priv->gt.stop_engine(engine);
- }
+       int ret;
  
- int
- i915_gem_suspend(struct drm_device *dev)
- {
-       struct drm_i915_private *dev_priv = to_i915(dev);
-       int ret = 0;
+       intel_suspend_gt_powersave(dev_priv);
  
        mutex_lock(&dev->struct_mutex);
-       ret = i915_gem_wait_for_idle(dev_priv);
+       /* We have to flush all the executing contexts to main memory so
+        * that they can saved in the hibernation image. To ensure the last
+        * context image is coherent, we have to switch away from it. That
+        * leaves the dev_priv->kernel_context still active when
+        * we actually suspend, and its image in memory may not match the GPU
+        * state. Fortunately, the kernel_context is disposable and we do
+        * not rely on its state.
+        */
+       ret = i915_gem_switch_to_kernel_context(dev_priv);
+       if (ret)
+               goto err;
+       ret = i915_gem_wait_for_idle(dev_priv,
+                                    I915_WAIT_INTERRUPTIBLE |
+                                    I915_WAIT_LOCKED);
        if (ret)
                goto err;
  
        i915_gem_retire_requests(dev_priv);
  
-       i915_gem_stop_engines(dev);
        i915_gem_context_lost(dev_priv);
        mutex_unlock(&dev->struct_mutex);
  
        return ret;
  }
  
+ void i915_gem_resume(struct drm_device *dev)
+ {
+       struct drm_i915_private *dev_priv = to_i915(dev);
+       mutex_lock(&dev->struct_mutex);
+       i915_gem_restore_gtt_mappings(dev);
+       /* As we didn't flush the kernel context before suspend, we cannot
+        * guarantee that the context image is complete. So let's just reset
+        * it and start again.
+        */
+       dev_priv->gt.resume(dev_priv);
+       mutex_unlock(&dev->struct_mutex);
+ }
  void i915_gem_init_swizzling(struct drm_device *dev)
  {
        struct drm_i915_private *dev_priv = to_i915(dev);
@@@ -5060,53 -4343,6 +4343,6 @@@ static void init_unused_rings(struct dr
        }
  }
  
- int i915_gem_init_engines(struct drm_device *dev)
- {
-       struct drm_i915_private *dev_priv = to_i915(dev);
-       int ret;
-       ret = intel_init_render_ring_buffer(dev);
-       if (ret)
-               return ret;
-       if (HAS_BSD(dev)) {
-               ret = intel_init_bsd_ring_buffer(dev);
-               if (ret)
-                       goto cleanup_render_ring;
-       }
-       if (HAS_BLT(dev)) {
-               ret = intel_init_blt_ring_buffer(dev);
-               if (ret)
-                       goto cleanup_bsd_ring;
-       }
-       if (HAS_VEBOX(dev)) {
-               ret = intel_init_vebox_ring_buffer(dev);
-               if (ret)
-                       goto cleanup_blt_ring;
-       }
-       if (HAS_BSD2(dev)) {
-               ret = intel_init_bsd2_ring_buffer(dev);
-               if (ret)
-                       goto cleanup_vebox_ring;
-       }
-       return 0;
- cleanup_vebox_ring:
-       intel_cleanup_engine(&dev_priv->engine[VECS]);
- cleanup_blt_ring:
-       intel_cleanup_engine(&dev_priv->engine[BCS]);
- cleanup_bsd_ring:
-       intel_cleanup_engine(&dev_priv->engine[VCS]);
- cleanup_render_ring:
-       intel_cleanup_engine(&dev_priv->engine[RCS]);
-       return ret;
- }
  int
  i915_gem_init_hw(struct drm_device *dev)
  {
        return ret;
  }
  
+ bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
+ {
+       if (INTEL_INFO(dev_priv)->gen < 6)
+               return false;
+       /* TODO: make semaphores and Execlists play nicely together */
+       if (i915.enable_execlists)
+               return false;
+       if (value >= 0)
+               return value;
+ #ifdef CONFIG_INTEL_IOMMU
+       /* Enable semaphores on SNB when IO remapping is off */
+       if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
+               return false;
+ #endif
+       return true;
+ }
  int i915_gem_init(struct drm_device *dev)
  {
        struct drm_i915_private *dev_priv = to_i915(dev);
        mutex_lock(&dev->struct_mutex);
  
        if (!i915.enable_execlists) {
-               dev_priv->gt.execbuf_submit = i915_gem_ringbuffer_submission;
-               dev_priv->gt.init_engines = i915_gem_init_engines;
-               dev_priv->gt.cleanup_engine = intel_cleanup_engine;
-               dev_priv->gt.stop_engine = intel_stop_engine;
+               dev_priv->gt.resume = intel_legacy_submission_resume;
+               dev_priv->gt.cleanup_engine = intel_engine_cleanup;
        } else {
-               dev_priv->gt.execbuf_submit = intel_execlists_submission;
-               dev_priv->gt.init_engines = intel_logical_rings_init;
+               dev_priv->gt.resume = intel_lr_context_resume;
                dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
-               dev_priv->gt.stop_engine = intel_logical_ring_stop;
        }
  
        /* This is just a security blanket to placate dragons.
        intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  
        i915_gem_init_userptr(dev_priv);
-       i915_gem_init_ggtt(dev);
+       ret = i915_gem_init_ggtt(dev_priv);
+       if (ret)
+               goto out_unlock;
  
        ret = i915_gem_context_init(dev);
        if (ret)
                goto out_unlock;
  
-       ret = dev_priv->gt.init_engines(dev);
+       ret = intel_engines_init(dev);
        if (ret)
                goto out_unlock;
  
        ret = i915_gem_init_hw(dev);
        if (ret == -EIO) {
-               /* Allow ring initialisation to fail by marking the GPU as
+               /* Allow engine initialisation to fail by marking the GPU as
                 * wedged. But we only want to do this where the GPU is angry,
                 * for all other failure, such as an allocation failure, bail.
                 */
                DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
-               atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
+               i915_gem_set_wedged(dev_priv);
                ret = 0;
        }
  
@@@ -5242,7 -4498,6 +4498,6 @@@ i915_gem_cleanup_engines(struct drm_dev
  static void
  init_engine_lists(struct intel_engine_cs *engine)
  {
-       INIT_LIST_HEAD(&engine->active_list);
        INIT_LIST_HEAD(&engine->request_list);
  }
  
@@@ -5250,6 -4505,7 +4505,7 @@@ voi
  i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
  {
        struct drm_device *dev = &dev_priv->drm;
+       int i;
  
        if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
            !IS_CHERRYVIEW(dev_priv))
                                I915_READ(vgtif_reg(avail_rs.fence_num));
  
        /* Initialize fence registers to zero */
+       for (i = 0; i < dev_priv->num_fence_regs; i++) {
+               struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
+               fence->i915 = dev_priv;
+               fence->id = i;
+               list_add_tail(&fence->link, &dev_priv->mm.fence_list);
+       }
        i915_gem_restore_fences(dev);
  
        i915_gem_detect_bit_6_swizzle(dev);
@@@ -5289,18 -4552,17 +4552,17 @@@ i915_gem_load_init(struct drm_device *d
        dev_priv->requests =
                kmem_cache_create("i915_gem_request",
                                  sizeof(struct drm_i915_gem_request), 0,
-                                 SLAB_HWCACHE_ALIGN,
+                                 SLAB_HWCACHE_ALIGN |
+                                 SLAB_RECLAIM_ACCOUNT |
+                                 SLAB_DESTROY_BY_RCU,
                                  NULL);
  
-       INIT_LIST_HEAD(&dev_priv->vm_list);
        INIT_LIST_HEAD(&dev_priv->context_list);
        INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
        INIT_LIST_HEAD(&dev_priv->mm.bound_list);
        INIT_LIST_HEAD(&dev_priv->mm.fence_list);
        for (i = 0; i < I915_NUM_ENGINES; i++)
                init_engine_lists(&dev_priv->engine[i]);
-       for (i = 0; i < I915_MAX_NUM_FENCES; i++)
-               INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
        INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
                          i915_gem_retire_work_handler);
        INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
  
        dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  
-       INIT_LIST_HEAD(&dev_priv->mm.fence_list);
        init_waitqueue_head(&dev_priv->pending_flip_queue);
  
        dev_priv->mm.interruptible = true;
  
-       mutex_init(&dev_priv->fb_tracking.lock);
+       atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
+       spin_lock_init(&dev_priv->fb_tracking.lock);
  }
  
  void i915_gem_load_cleanup(struct drm_device *dev)
        kmem_cache_destroy(dev_priv->requests);
        kmem_cache_destroy(dev_priv->vmas);
        kmem_cache_destroy(dev_priv->objects);
+       /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
+       rcu_barrier();
+ }
+ int i915_gem_freeze(struct drm_i915_private *dev_priv)
+ {
+       intel_runtime_pm_get(dev_priv);
+       mutex_lock(&dev_priv->drm.struct_mutex);
+       i915_gem_shrink_all(dev_priv);
+       mutex_unlock(&dev_priv->drm.struct_mutex);
+       intel_runtime_pm_put(dev_priv);
+       return 0;
  }
  
  int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
  {
        struct drm_i915_gem_object *obj;
+       struct list_head *phases[] = {
+               &dev_priv->mm.unbound_list,
+               &dev_priv->mm.bound_list,
+               NULL
+       }, **p;
  
        /* Called just before we write the hibernation image.
         *
         *
         * To make sure the hibernation image contains the latest state,
         * we update that state just before writing out the image.
+        *
+        * To try and reduce the hibernation image, we manually shrink
+        * the objects as well.
         */
  
-       list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
-               obj->base.read_domains = I915_GEM_DOMAIN_CPU;
-               obj->base.write_domain = I915_GEM_DOMAIN_CPU;
-       }
+       mutex_lock(&dev_priv->drm.struct_mutex);
+       i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND);
  
-       list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
-               obj->base.read_domains = I915_GEM_DOMAIN_CPU;
-               obj->base.write_domain = I915_GEM_DOMAIN_CPU;
+       for (p = phases; *p; p++) {
+               list_for_each_entry(obj, *p, global_list) {
+                       obj->base.read_domains = I915_GEM_DOMAIN_CPU;
+                       obj->base.write_domain = I915_GEM_DOMAIN_CPU;
+               }
        }
+       mutex_unlock(&dev_priv->drm.struct_mutex);
  
        return 0;
  }
  void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  {
        struct drm_i915_file_private *file_priv = file->driver_priv;
+       struct drm_i915_gem_request *request;
  
        /* Clean up our request list when the client is going away, so that
         * later retire_requests won't dereference our soon-to-be-gone
         * file_priv.
         */
        spin_lock(&file_priv->mm.lock);
-       while (!list_empty(&file_priv->mm.request_list)) {
-               struct drm_i915_gem_request *request;
-               request = list_first_entry(&file_priv->mm.request_list,
-                                          struct drm_i915_gem_request,
-                                          client_list);
-               list_del(&request->client_list);
+       list_for_each_entry(request, &file_priv->mm.request_list, client_list)
                request->file_priv = NULL;
-       }
        spin_unlock(&file_priv->mm.lock);
  
        if (!list_empty(&file_priv->rps.link)) {
@@@ -5402,7 -4683,7 +4683,7 @@@ int i915_gem_open(struct drm_device *de
        spin_lock_init(&file_priv->mm.lock);
        INIT_LIST_HEAD(&file_priv->mm.request_list);
  
-       file_priv->bsd_ring = -1;
+       file_priv->bsd_engine = -1;
  
        ret = i915_gem_context_open(dev, file);
        if (ret)
@@@ -5424,118 -4705,24 +4705,24 @@@ void i915_gem_track_fb(struct drm_i915_
                       struct drm_i915_gem_object *new,
                       unsigned frontbuffer_bits)
  {
+       /* Control of individual bits within the mask are guarded by
+        * the owning plane->mutex, i.e. we can never see concurrent
+        * manipulation of individual bits. But since the bitfield as a whole
+        * is updated using RMW, we need to use atomics in order to update
+        * the bits.
+        */
+       BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
+                    sizeof(atomic_t) * BITS_PER_BYTE);
        if (old) {
-               WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
-               WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
-               old->frontbuffer_bits &= ~frontbuffer_bits;
+               WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
+               atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
        }
  
        if (new) {
-               WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
-               WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
-               new->frontbuffer_bits |= frontbuffer_bits;
-       }
- }
- /* All the new VM stuff */
- u64 i915_gem_obj_offset(struct drm_i915_gem_object *o,
-                       struct i915_address_space *vm)
- {
-       struct drm_i915_private *dev_priv = to_i915(o->base.dev);
-       struct i915_vma *vma;
-       WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
-       list_for_each_entry(vma, &o->vma_list, obj_link) {
-               if (vma->is_ggtt &&
-                   vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
-                       continue;
-               if (vma->vm == vm)
-                       return vma->node.start;
-       }
-       WARN(1, "%s vma for this object not found.\n",
-            i915_is_ggtt(vm) ? "global" : "ppgtt");
-       return -1;
- }
- u64 i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
-                                 const struct i915_ggtt_view *view)
- {
-       struct i915_vma *vma;
-       list_for_each_entry(vma, &o->vma_list, obj_link)
-               if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
-                       return vma->node.start;
-       WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
-       return -1;
- }
- bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
-                       struct i915_address_space *vm)
- {
-       struct i915_vma *vma;
-       list_for_each_entry(vma, &o->vma_list, obj_link) {
-               if (vma->is_ggtt &&
-                   vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
-                       continue;
-               if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
-                       return true;
+               WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
+               atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
        }
-       return false;
- }
- bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
-                                 const struct i915_ggtt_view *view)
- {
-       struct i915_vma *vma;
-       list_for_each_entry(vma, &o->vma_list, obj_link)
-               if (vma->is_ggtt &&
-                   i915_ggtt_view_equal(&vma->ggtt_view, view) &&
-                   drm_mm_node_allocated(&vma->node))
-                       return true;
-       return false;
- }
- bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
- {
-       struct i915_vma *vma;
-       list_for_each_entry(vma, &o->vma_list, obj_link)
-               if (drm_mm_node_allocated(&vma->node))
-                       return true;
-       return false;
- }
- unsigned long i915_gem_obj_ggtt_size(struct drm_i915_gem_object *o)
- {
-       struct i915_vma *vma;
-       GEM_BUG_ON(list_empty(&o->vma_list));
-       list_for_each_entry(vma, &o->vma_list, obj_link) {
-               if (vma->is_ggtt &&
-                   vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
-                       return vma->node.size;
-       }
-       return 0;
- }
- bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
- {
-       struct i915_vma *vma;
-       list_for_each_entry(vma, &obj->vma_list, obj_link)
-               if (vma->pin_count > 0)
-                       return true;
-       return false;
  }
  
  /* Like i915_gem_object_get_page(), but mark the returned page dirty */
@@@ -5590,6 -4777,6 +4777,6 @@@ i915_gem_object_create_from_data(struc
        return obj;
  
  fail:
-       drm_gem_object_unreference(&obj->base);
+       i915_gem_object_put(obj);
        return ERR_PTR(ret);
  }
   *
   */
  
+ #include <linux/dma_remapping.h>
+ #include <linux/reservation.h>
+ #include <linux/uaccess.h>
  #include <drm/drmP.h>
  #include <drm/i915_drm.h>
  #include "i915_drv.h"
+ #include "i915_gem_dmabuf.h"
  #include "i915_trace.h"
  #include "intel_drv.h"
- #include <linux/dma_remapping.h>
- #include <linux/uaccess.h>
+ #include "intel_frontbuffer.h"
  
- #define  __EXEC_OBJECT_HAS_PIN (1<<31)
- #define  __EXEC_OBJECT_HAS_FENCE (1<<30)
- #define  __EXEC_OBJECT_NEEDS_MAP (1<<29)
- #define  __EXEC_OBJECT_NEEDS_BIAS (1<<28)
+ #define DBG_USE_CPU_RELOC 0 /* -1 force GTT relocs; 1 force CPU relocs */
+ #define  __EXEC_OBJECT_HAS_PIN                (1<<31)
+ #define  __EXEC_OBJECT_HAS_FENCE      (1<<30)
+ #define  __EXEC_OBJECT_NEEDS_MAP      (1<<29)
+ #define  __EXEC_OBJECT_NEEDS_BIAS     (1<<28)
+ #define  __EXEC_OBJECT_INTERNAL_FLAGS (0xf<<28) /* all of the above */
  
  #define BATCH_OFFSET_BIAS (256*1024)
  
+ struct i915_execbuffer_params {
+       struct drm_device               *dev;
+       struct drm_file                 *file;
+       struct i915_vma                 *batch;
+       u32                             dispatch_flags;
+       u32                             args_batch_start_offset;
+       struct intel_engine_cs          *engine;
+       struct i915_gem_context         *ctx;
+       struct drm_i915_gem_request     *request;
+ };
  struct eb_vmas {
+       struct drm_i915_private *i915;
        struct list_head vmas;
        int and;
        union {
@@@ -51,7 -71,8 +71,8 @@@
  };
  
  static struct eb_vmas *
- eb_create(struct drm_i915_gem_execbuffer2 *args)
+ eb_create(struct drm_i915_private *i915,
+         struct drm_i915_gem_execbuffer2 *args)
  {
        struct eb_vmas *eb = NULL;
  
@@@ -78,6 -99,7 +99,7 @@@
        } else
                eb->and = -args->buffer_count;
  
+       eb->i915 = i915;
        INIT_LIST_HEAD(&eb->vmas);
        return eb;
  }
@@@ -89,6 -111,26 +111,26 @@@ eb_reset(struct eb_vmas *eb
                memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
  }
  
+ static struct i915_vma *
+ eb_get_batch(struct eb_vmas *eb)
+ {
+       struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
+       /*
+        * SNA is doing fancy tricks with compressing batch buffers, which leads
+        * to negative relocation deltas. Usually that works out ok since the
+        * relocate address is still positive, except when the batch is placed
+        * very low in the GTT. Ensure this doesn't happen.
+        *
+        * Note that actual hangs have only been observed on gen7, but for
+        * paranoia do it everywhere.
+        */
+       if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
+               vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
+       return vma;
+ }
  static int
  eb_lookup_vmas(struct eb_vmas *eb,
               struct drm_i915_gem_exec_object2 *exec,
                        goto err;
                }
  
-               drm_gem_object_reference(&obj->base);
+               i915_gem_object_get(obj);
                list_add_tail(&obj->obj_exec_link, &objects);
        }
        spin_unlock(&file->table_lock);
                 * from the (obj, vm) we don't run the risk of creating
                 * duplicated vmas for the same vm.
                 */
-               vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
-               if (IS_ERR(vma)) {
+               vma = i915_gem_obj_lookup_or_create_vma(obj, vm, NULL);
+               if (unlikely(IS_ERR(vma))) {
                        DRM_DEBUG("Failed to lookup VMA\n");
                        ret = PTR_ERR(vma);
                        goto err;
@@@ -175,7 -217,7 +217,7 @@@ err
                                       struct drm_i915_gem_object,
                                       obj_exec_link);
                list_del_init(&obj->obj_exec_link);
-               drm_gem_object_unreference(&obj->base);
+               i915_gem_object_put(obj);
        }
        /*
         * Objects already transfered to the vmas list will be unreferenced by
@@@ -208,7 -250,6 +250,6 @@@ static voi
  i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
  {
        struct drm_i915_gem_exec_object2 *entry;
-       struct drm_i915_gem_object *obj = vma->obj;
  
        if (!drm_mm_node_allocated(&vma->node))
                return;
        entry = vma->exec_entry;
  
        if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
-               i915_gem_object_unpin_fence(obj);
+               i915_vma_unpin_fence(vma);
  
        if (entry->flags & __EXEC_OBJECT_HAS_PIN)
-               vma->pin_count--;
+               __i915_vma_unpin(vma);
  
        entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
  }
@@@ -234,13 -275,19 +275,19 @@@ static void eb_destroy(struct eb_vmas *
                                       exec_list);
                list_del_init(&vma->exec_list);
                i915_gem_execbuffer_unreserve_vma(vma);
-               drm_gem_object_unreference(&vma->obj->base);
+               i915_vma_put(vma);
        }
        kfree(eb);
  }
  
  static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
  {
+       if (!i915_gem_object_has_struct_page(obj))
+               return false;
+       if (DBG_USE_CPU_RELOC)
+               return DBG_USE_CPU_RELOC > 0;
        return (HAS_LLC(obj->base.dev) ||
                obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
                obj->cache_level != I915_CACHE_NONE);
@@@ -265,144 -312,265 +312,265 @@@ static inline uint64_t gen8_noncanonica
  }
  
  static inline uint64_t
- relocation_target(struct drm_i915_gem_relocation_entry *reloc,
+ relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
                  uint64_t target_offset)
  {
        return gen8_canonical_addr((int)reloc->delta + target_offset);
  }
  
- static int
- relocate_entry_cpu(struct drm_i915_gem_object *obj,
-                  struct drm_i915_gem_relocation_entry *reloc,
-                  uint64_t target_offset)
+ struct reloc_cache {
+       struct drm_i915_private *i915;
+       struct drm_mm_node node;
+       unsigned long vaddr;
+       unsigned int page;
+       bool use_64bit_reloc;
+ };
+ static void reloc_cache_init(struct reloc_cache *cache,
+                            struct drm_i915_private *i915)
  {
-       struct drm_device *dev = obj->base.dev;
-       uint32_t page_offset = offset_in_page(reloc->offset);
-       uint64_t delta = relocation_target(reloc, target_offset);
-       char *vaddr;
-       int ret;
+       cache->page = -1;
+       cache->vaddr = 0;
+       cache->i915 = i915;
+       cache->use_64bit_reloc = INTEL_GEN(cache->i915) >= 8;
+       cache->node.allocated = false;
+ }
  
-       ret = i915_gem_object_set_to_cpu_domain(obj, true);
-       if (ret)
-               return ret;
+ static inline void *unmask_page(unsigned long p)
+ {
+       return (void *)(uintptr_t)(p & PAGE_MASK);
+ }
+ static inline unsigned int unmask_flags(unsigned long p)
+ {
+       return p & ~PAGE_MASK;
+ }
+ #define KMAP 0x4 /* after CLFLUSH_FLAGS */
+ static void reloc_cache_fini(struct reloc_cache *cache)
+ {
+       void *vaddr;
  
-       vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
-                               reloc->offset >> PAGE_SHIFT));
-       *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
+       if (!cache->vaddr)
+               return;
  
-       if (INTEL_INFO(dev)->gen >= 8) {
-               page_offset = offset_in_page(page_offset + sizeof(uint32_t));
+       vaddr = unmask_page(cache->vaddr);
+       if (cache->vaddr & KMAP) {
+               if (cache->vaddr & CLFLUSH_AFTER)
+                       mb();
  
-               if (page_offset == 0) {
-                       kunmap_atomic(vaddr);
-                       vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
-                           (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
+               kunmap_atomic(vaddr);
+               i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
+       } else {
+               wmb();
+               io_mapping_unmap_atomic((void __iomem *)vaddr);
+               if (cache->node.allocated) {
+                       struct i915_ggtt *ggtt = &cache->i915->ggtt;
+                       ggtt->base.clear_range(&ggtt->base,
+                                              cache->node.start,
+                                              cache->node.size,
+                                              true);
+                       drm_mm_remove_node(&cache->node);
+               } else {
+                       i915_vma_unpin((struct i915_vma *)cache->node.mm);
                }
+       }
+ }
+ static void *reloc_kmap(struct drm_i915_gem_object *obj,
+                       struct reloc_cache *cache,
+                       int page)
+ {
+       void *vaddr;
+       if (cache->vaddr) {
+               kunmap_atomic(unmask_page(cache->vaddr));
+       } else {
+               unsigned int flushes;
+               int ret;
+               ret = i915_gem_obj_prepare_shmem_write(obj, &flushes);
+               if (ret)
+                       return ERR_PTR(ret);
+               BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
+               BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
  
-               *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
+               cache->vaddr = flushes | KMAP;
+               cache->node.mm = (void *)obj;
+               if (flushes)
+                       mb();
        }
  
-       kunmap_atomic(vaddr);
+       vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
+       cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
+       cache->page = page;
  
-       return 0;
+       return vaddr;
  }
  
- static int
- relocate_entry_gtt(struct drm_i915_gem_object *obj,
-                  struct drm_i915_gem_relocation_entry *reloc,
-                  uint64_t target_offset)
+ static void *reloc_iomap(struct drm_i915_gem_object *obj,
+                        struct reloc_cache *cache,
+                        int page)
  {
-       struct drm_device *dev = obj->base.dev;
-       struct drm_i915_private *dev_priv = to_i915(dev);
-       struct i915_ggtt *ggtt = &dev_priv->ggtt;
-       uint64_t delta = relocation_target(reloc, target_offset);
-       uint64_t offset;
-       void __iomem *reloc_page;
-       int ret;
+       struct i915_ggtt *ggtt = &cache->i915->ggtt;
+       unsigned long offset;
+       void *vaddr;
+       if (cache->node.allocated) {
+               wmb();
+               ggtt->base.insert_page(&ggtt->base,
+                                      i915_gem_object_get_dma_address(obj, page),
+                                      cache->node.start, I915_CACHE_NONE, 0);
+               cache->page = page;
+               return unmask_page(cache->vaddr);
+       }
  
-       ret = i915_gem_object_set_to_gtt_domain(obj, true);
-       if (ret)
-               return ret;
+       if (cache->vaddr) {
+               io_mapping_unmap_atomic(unmask_page(cache->vaddr));
+       } else {
+               struct i915_vma *vma;
+               int ret;
  
-       ret = i915_gem_object_put_fence(obj);
-       if (ret)
-               return ret;
+               if (use_cpu_reloc(obj))
+                       return NULL;
+               ret = i915_gem_object_set_to_gtt_domain(obj, true);
+               if (ret)
+                       return ERR_PTR(ret);
+               vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
+                                              PIN_MAPPABLE | PIN_NONBLOCK);
+               if (IS_ERR(vma)) {
+                       memset(&cache->node, 0, sizeof(cache->node));
+                       ret = drm_mm_insert_node_in_range_generic
+                               (&ggtt->base.mm, &cache->node,
+                                4096, 0, 0,
+                                0, ggtt->mappable_end,
+                                DRM_MM_SEARCH_DEFAULT,
+                                DRM_MM_CREATE_DEFAULT);
+                       if (ret) /* no inactive aperture space, use cpu reloc */
+                               return NULL;
+               } else {
+                       ret = i915_vma_put_fence(vma);
+                       if (ret) {
+                               i915_vma_unpin(vma);
+                               return ERR_PTR(ret);
+                       }
  
-       /* Map the page containing the relocation we're going to perform.  */
-       offset = i915_gem_obj_ggtt_offset(obj);
-       offset += reloc->offset;
-       reloc_page = io_mapping_map_atomic_wc(ggtt->mappable,
-                                             offset & PAGE_MASK);
-       iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
-       if (INTEL_INFO(dev)->gen >= 8) {
-               offset += sizeof(uint32_t);
-               if (offset_in_page(offset) == 0) {
-                       io_mapping_unmap_atomic(reloc_page);
-                       reloc_page =
-                               io_mapping_map_atomic_wc(ggtt->mappable,
-                                                        offset);
+                       cache->node.start = vma->node.start;
+                       cache->node.mm = (void *)vma;
                }
+       }
  
-               iowrite32(upper_32_bits(delta),
-                         reloc_page + offset_in_page(offset));
+       offset = cache->node.start;
+       if (cache->node.allocated) {
+               ggtt->base.insert_page(&ggtt->base,
+                                      i915_gem_object_get_dma_address(obj, page),
+                                      offset, I915_CACHE_NONE, 0);
+       } else {
+               offset += page << PAGE_SHIFT;
        }
  
-       io_mapping_unmap_atomic(reloc_page);
+       vaddr = io_mapping_map_atomic_wc(&cache->i915->ggtt.mappable, offset);
+       cache->page = page;
+       cache->vaddr = (unsigned long)vaddr;
  
-       return 0;
+       return vaddr;
  }
  
- static void
- clflush_write32(void *addr, uint32_t value)
+ static void *reloc_vaddr(struct drm_i915_gem_object *obj,
+                        struct reloc_cache *cache,
+                        int page)
  {
-       /* This is not a fast path, so KISS. */
-       drm_clflush_virt_range(addr, sizeof(uint32_t));
-       *(uint32_t *)addr = value;
-       drm_clflush_virt_range(addr, sizeof(uint32_t));
+       void *vaddr;
+       if (cache->page == page) {
+               vaddr = unmask_page(cache->vaddr);
+       } else {
+               vaddr = NULL;
+               if ((cache->vaddr & KMAP) == 0)
+                       vaddr = reloc_iomap(obj, cache, page);
+               if (!vaddr)
+                       vaddr = reloc_kmap(obj, cache, page);
+       }
+       return vaddr;
  }
  
- static int
- relocate_entry_clflush(struct drm_i915_gem_object *obj,
-                      struct drm_i915_gem_relocation_entry *reloc,
-                      uint64_t target_offset)
+ static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
  {
-       struct drm_device *dev = obj->base.dev;
-       uint32_t page_offset = offset_in_page(reloc->offset);
-       uint64_t delta = relocation_target(reloc, target_offset);
-       char *vaddr;
-       int ret;
+       if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
+               if (flushes & CLFLUSH_BEFORE) {
+                       clflushopt(addr);
+                       mb();
+               }
  
-       ret = i915_gem_object_set_to_gtt_domain(obj, true);
-       if (ret)
-               return ret;
+               *addr = value;
  
-       vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
-                               reloc->offset >> PAGE_SHIFT));
-       clflush_write32(vaddr + page_offset, lower_32_bits(delta));
+               /* Writes to the same cacheline are serialised by the CPU
+                * (including clflush). On the write path, we only require
+                * that it hits memory in an orderly fashion and place
+                * mb barriers at the start and end of the relocation phase
+                * to ensure ordering of clflush wrt to the system.
+                */
+               if (flushes & CLFLUSH_AFTER)
+                       clflushopt(addr);
+       } else
+               *addr = value;
+ }
  
-       if (INTEL_INFO(dev)->gen >= 8) {
-               page_offset = offset_in_page(page_offset + sizeof(uint32_t));
+ static int
+ relocate_entry(struct drm_i915_gem_object *obj,
+              const struct drm_i915_gem_relocation_entry *reloc,
+              struct reloc_cache *cache,
+              u64 target_offset)
+ {
+       u64 offset = reloc->offset;
+       bool wide = cache->use_64bit_reloc;
+       void *vaddr;
+       target_offset = relocation_target(reloc, target_offset);
+ repeat:
+       vaddr = reloc_vaddr(obj, cache, offset >> PAGE_SHIFT);
+       if (IS_ERR(vaddr))
+               return PTR_ERR(vaddr);
+       clflush_write32(vaddr + offset_in_page(offset),
+                       lower_32_bits(target_offset),
+                       cache->vaddr);
+       if (wide) {
+               offset += sizeof(u32);
+               target_offset >>= 32;
+               wide = false;
+               goto repeat;
+       }
  
-               if (page_offset == 0) {
-                       kunmap_atomic(vaddr);
-                       vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
-                           (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
-               }
+       return 0;
+ }
  
-               clflush_write32(vaddr + page_offset, upper_32_bits(delta));
-       }
+ static bool object_is_idle(struct drm_i915_gem_object *obj)
+ {
+       unsigned long active = i915_gem_object_get_active(obj);
+       int idx;
  
-       kunmap_atomic(vaddr);
+       for_each_active(active, idx) {
+               if (!i915_gem_active_is_idle(&obj->last_read[idx],
+                                            &obj->base.dev->struct_mutex))
+                       return false;
+       }
  
-       return 0;
+       return true;
  }
  
  static int
  i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
                                   struct eb_vmas *eb,
-                                  struct drm_i915_gem_relocation_entry *reloc)
+                                  struct drm_i915_gem_relocation_entry *reloc,
+                                  struct reloc_cache *cache)
  {
        struct drm_device *dev = obj->base.dev;
        struct drm_gem_object *target_obj;
  
        /* Check that the relocation address is valid... */
        if (unlikely(reloc->offset >
-               obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
+                    obj->base.size - (cache->use_64bit_reloc ? 8 : 4))) {
                DRM_DEBUG("Relocation beyond object bounds: "
                          "obj %p target %d offset %d size %d.\n",
                          obj, reloc->target_handle,
        }
  
        /* We can't wait for rendering with pagefaults disabled */
-       if (obj->active && pagefault_disabled())
+       if (pagefault_disabled() && !object_is_idle(obj))
                return -EFAULT;
  
-       if (use_cpu_reloc(obj))
-               ret = relocate_entry_cpu(obj, reloc, target_offset);
-       else if (obj->map_and_fenceable)
-               ret = relocate_entry_gtt(obj, reloc, target_offset);
-       else if (static_cpu_has(X86_FEATURE_CLFLUSH))
-               ret = relocate_entry_clflush(obj, reloc, target_offset);
-       else {
-               WARN_ONCE(1, "Impossible case in relocation handling\n");
-               ret = -ENODEV;
-       }
+       ret = relocate_entry(obj, reloc, cache, target_offset);
        if (ret)
                return ret;
  
        /* and update the user's relocation entry */
        reloc->presumed_offset = target_offset;
        return 0;
  }
  
@@@ -513,9 -670,11 +670,11 @@@ i915_gem_execbuffer_relocate_vma(struc
        struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
        struct drm_i915_gem_relocation_entry __user *user_relocs;
        struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
-       int remain, ret;
+       struct reloc_cache cache;
+       int remain, ret = 0;
  
        user_relocs = u64_to_user_ptr(entry->relocs_ptr);
+       reloc_cache_init(&cache, eb->i915);
  
        remain = entry->relocation_count;
        while (remain) {
                        count = ARRAY_SIZE(stack_reloc);
                remain -= count;
  
-               if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
-                       return -EFAULT;
+               if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0]))) {
+                       ret = -EFAULT;
+                       goto out;
+               }
  
                do {
                        u64 offset = r->presumed_offset;
  
-                       ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
+                       ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r, &cache);
                        if (ret)
-                               return ret;
+                               goto out;
  
                        if (r->presumed_offset != offset &&
-                           __put_user(r->presumed_offset, &user_relocs->presumed_offset)) {
-                               return -EFAULT;
+                           __put_user(r->presumed_offset,
+                                      &user_relocs->presumed_offset)) {
+                               ret = -EFAULT;
+                               goto out;
                        }
  
                        user_relocs++;
                } while (--count);
        }
  
-       return 0;
+ out:
+       reloc_cache_fini(&cache);
+       return ret;
  #undef N_RELOC
  }
  
@@@ -555,15 -720,18 +720,18 @@@ i915_gem_execbuffer_relocate_vma_slow(s
                                      struct drm_i915_gem_relocation_entry *relocs)
  {
        const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
-       int i, ret;
+       struct reloc_cache cache;
+       int i, ret = 0;
  
+       reloc_cache_init(&cache, eb->i915);
        for (i = 0; i < entry->relocation_count; i++) {
-               ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
+               ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i], &cache);
                if (ret)
-                       return ret;
+                       break;
        }
+       reloc_cache_fini(&cache);
  
-       return 0;
+       return ret;
  }
  
  static int
@@@ -626,23 -794,27 +794,27 @@@ i915_gem_execbuffer_reserve_vma(struct 
                        flags |= PIN_HIGH;
        }
  
-       ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
-       if ((ret == -ENOSPC  || ret == -E2BIG) &&
+       ret = i915_vma_pin(vma,
+                          entry->pad_to_size,
+                          entry->alignment,
+                          flags);
+       if ((ret == -ENOSPC || ret == -E2BIG) &&
            only_mappable_for_reloc(entry->flags))
-               ret = i915_gem_object_pin(obj, vma->vm,
-                                         entry->alignment,
-                                         flags & ~PIN_MAPPABLE);
+               ret = i915_vma_pin(vma,
+                                  entry->pad_to_size,
+                                  entry->alignment,
+                                  flags & ~PIN_MAPPABLE);
        if (ret)
                return ret;
  
        entry->flags |= __EXEC_OBJECT_HAS_PIN;
  
        if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
-               ret = i915_gem_object_get_fence(obj);
+               ret = i915_vma_get_fence(vma);
                if (ret)
                        return ret;
  
-               if (i915_gem_object_pin_fence(obj))
+               if (i915_vma_pin_fence(vma))
                        entry->flags |= __EXEC_OBJECT_HAS_FENCE;
        }
  
@@@ -667,7 -839,7 +839,7 @@@ need_reloc_mappable(struct i915_vma *vm
        if (entry->relocation_count == 0)
                return false;
  
-       if (!vma->is_ggtt)
+       if (!i915_vma_is_ggtt(vma))
                return false;
  
        /* See also use_cpu_reloc() */
@@@ -684,14 -856,17 +856,17 @@@ static boo
  eb_vma_misplaced(struct i915_vma *vma)
  {
        struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
-       struct drm_i915_gem_object *obj = vma->obj;
  
-       WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP && !vma->is_ggtt);
+       WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
+               !i915_vma_is_ggtt(vma));
  
        if (entry->alignment &&
            vma->node.start & (entry->alignment - 1))
                return true;
  
+       if (vma->node.size < entry->pad_to_size)
+               return true;
        if (entry->flags & EXEC_OBJECT_PINNED &&
            vma->node.start != entry->offset)
                return true;
                return true;
  
        /* avoid costly ping-pong once a batch bo ended up non-mappable */
-       if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
+       if (entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
+           !i915_vma_is_map_and_fenceable(vma))
                return !only_mappable_for_reloc(entry->flags);
  
        if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
@@@ -725,8 -901,6 +901,6 @@@ i915_gem_execbuffer_reserve(struct inte
        bool has_fenced_gpu_access = INTEL_GEN(engine->i915) < 4;
        int retry;
  
-       i915_gem_retire_requests_ring(engine);
        vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
  
        INIT_LIST_HEAD(&ordered_vmas);
                        entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
                need_fence =
                        entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
-                       obj->tiling_mode != I915_TILING_NONE;
+                       i915_gem_object_is_tiled(obj);
                need_mappable = need_fence || need_reloc_mappable(vma);
  
                if (entry->flags & EXEC_OBJECT_PINNED)
@@@ -843,7 -1017,7 +1017,7 @@@ i915_gem_execbuffer_relocate_slow(struc
                vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
                list_del_init(&vma->exec_list);
                i915_gem_execbuffer_unreserve_vma(vma);
-               drm_gem_object_unreference(&vma->obj->base);
+               i915_vma_put(vma);
        }
  
        mutex_unlock(&dev->struct_mutex);
        return ret;
  }
  
+ static unsigned int eb_other_engines(struct drm_i915_gem_request *req)
+ {
+       unsigned int mask;
+       mask = ~intel_engine_flag(req->engine) & I915_BO_ACTIVE_MASK;
+       mask <<= I915_BO_ACTIVE_SHIFT;
+       return mask;
+ }
  static int
  i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
                                struct list_head *vmas)
  {
-       const unsigned other_rings = ~intel_engine_flag(req->engine);
+       const unsigned int other_rings = eb_other_engines(req);
        struct i915_vma *vma;
        int ret;
  
        list_for_each_entry(vma, vmas, exec_list) {
                struct drm_i915_gem_object *obj = vma->obj;
+               struct reservation_object *resv;
  
-               if (obj->active & other_rings) {
-                       ret = i915_gem_object_sync(obj, req->engine, &req);
+               if (obj->flags & other_rings) {
+                       ret = i915_gem_request_await_object
+                               (req, obj, obj->base.pending_write_domain);
                        if (ret)
                                return ret;
                }
  
+               resv = i915_gem_object_get_dmabuf_resv(obj);
+               if (resv) {
+                       ret = i915_sw_fence_await_reservation
+                               (&req->submit, resv, &i915_fence_ops,
+                                obj->base.pending_write_domain, 10*HZ,
+                                GFP_KERNEL | __GFP_NOWARN);
+                       if (ret < 0)
+                               return ret;
+               }
                if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
                        i915_gem_clflush_object(obj, false);
        }
        /* Unconditionally flush any chipset caches (for streaming writes). */
        i915_gem_chipset_flush(req->engine->i915);
  
-       /* Unconditionally invalidate gpu caches and ensure that we do flush
-        * any residual writes from the previous batch.
-        */
-       return intel_ring_invalidate_all_caches(req);
+       /* Unconditionally invalidate GPU caches and TLBs. */
+       return req->engine->emit_flush(req, EMIT_INVALIDATE);
  }
  
  static bool
@@@ -1000,6 -1194,9 +1194,9 @@@ validate_exec_list(struct drm_device *d
        unsigned invalid_flags;
        int i;
  
+       /* INTERNAL flags must not overlap with external ones */
+       BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & ~__EXEC_OBJECT_UNKNOWN_FLAGS);
        invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
        if (USES_FULL_PPGTT(dev))
                invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
                if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
                        return -EINVAL;
  
+               /* pad_to_size was once a reserved field, so sanitize it */
+               if (exec[i].flags & EXEC_OBJECT_PAD_TO_SIZE) {
+                       if (offset_in_page(exec[i].pad_to_size))
+                               return -EINVAL;
+               } else {
+                       exec[i].pad_to_size = 0;
+               }
                /* First check for malicious input causing overflow in
                 * the worst case where we need to allocate the entire
                 * relocation tree as a single array.
                        return -EFAULT;
  
                if (likely(!i915.prefault_disable)) {
 -                      if (fault_in_multipages_readable(ptr, length))
 +                      if (fault_in_pages_readable(ptr, length))
                                return -EFAULT;
                }
        }
@@@ -1060,12 -1265,9 +1265,9 @@@ static struct i915_gem_context 
  i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
                          struct intel_engine_cs *engine, const u32 ctx_id)
  {
-       struct i915_gem_context *ctx = NULL;
+       struct i915_gem_context *ctx;
        struct i915_ctx_hang_stats *hs;
  
-       if (engine->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
-               return ERR_PTR(-EINVAL);
        ctx = i915_gem_context_lookup(file->driver_priv, ctx_id);
        if (IS_ERR(ctx))
                return ctx;
        return ctx;
  }
  
- void
+ void i915_vma_move_to_active(struct i915_vma *vma,
+                            struct drm_i915_gem_request *req,
+                            unsigned int flags)
+ {
+       struct drm_i915_gem_object *obj = vma->obj;
+       const unsigned int idx = req->engine->id;
+       GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
+       obj->dirty = 1; /* be paranoid  */
+       /* Add a reference if we're newly entering the active list.
+        * The order in which we add operations to the retirement queue is
+        * vital here: mark_active adds to the start of the callback list,
+        * such that subsequent callbacks are called first. Therefore we
+        * add the active reference first and queue for it to be dropped
+        * *last*.
+        */
+       if (!i915_gem_object_is_active(obj))
+               i915_gem_object_get(obj);
+       i915_gem_object_set_active(obj, idx);
+       i915_gem_active_set(&obj->last_read[idx], req);
+       if (flags & EXEC_OBJECT_WRITE) {
+               i915_gem_active_set(&obj->last_write, req);
+               intel_fb_obj_invalidate(obj, ORIGIN_CS);
+               /* update for the implicit flush after a batch */
+               obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
+       }
+       if (flags & EXEC_OBJECT_NEEDS_FENCE)
+               i915_gem_active_set(&vma->last_fence, req);
+       i915_vma_set_active(vma, idx);
+       i915_gem_active_set(&vma->last_read[idx], req);
+       list_move_tail(&vma->vm_link, &vma->vm->active_list);
+ }
+ static void eb_export_fence(struct drm_i915_gem_object *obj,
+                           struct drm_i915_gem_request *req,
+                           unsigned int flags)
+ {
+       struct reservation_object *resv;
+       resv = i915_gem_object_get_dmabuf_resv(obj);
+       if (!resv)
+               return;
+       /* Ignore errors from failing to allocate the new fence, we can't
+        * handle an error right now. Worst case should be missed
+        * synchronisation leading to rendering corruption.
+        */
+       ww_mutex_lock(&resv->lock, NULL);
+       if (flags & EXEC_OBJECT_WRITE)
+               reservation_object_add_excl_fence(resv, &req->fence);
+       else if (reservation_object_reserve_shared(resv) == 0)
+               reservation_object_add_shared_fence(resv, &req->fence);
+       ww_mutex_unlock(&resv->lock);
+ }
+ static void
  i915_gem_execbuffer_move_to_active(struct list_head *vmas,
                                   struct drm_i915_gem_request *req)
  {
-       struct intel_engine_cs *engine = i915_gem_request_get_engine(req);
        struct i915_vma *vma;
  
        list_for_each_entry(vma, vmas, exec_list) {
-               struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
                struct drm_i915_gem_object *obj = vma->obj;
                u32 old_read = obj->base.read_domains;
                u32 old_write = obj->base.write_domain;
  
-               obj->dirty = 1; /* be paranoid  */
                obj->base.write_domain = obj->base.pending_write_domain;
-               if (obj->base.write_domain == 0)
+               if (obj->base.write_domain)
+                       vma->exec_entry->flags |= EXEC_OBJECT_WRITE;
+               else
                        obj->base.pending_read_domains |= obj->base.read_domains;
                obj->base.read_domains = obj->base.pending_read_domains;
  
-               i915_vma_move_to_active(vma, req);
-               if (obj->base.write_domain) {
-                       i915_gem_request_assign(&obj->last_write_req, req);
-                       intel_fb_obj_invalidate(obj, ORIGIN_CS);
-                       /* update for the implicit flush after a batch */
-                       obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
-               }
-               if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
-                       i915_gem_request_assign(&obj->last_fenced_req, req);
-                       if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
-                               struct drm_i915_private *dev_priv = engine->i915;
-                               list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
-                                              &dev_priv->mm.fence_list);
-                       }
-               }
+               i915_vma_move_to_active(vma, req, vma->exec_entry->flags);
+               eb_export_fence(obj, req, vma->exec_entry->flags);
                trace_i915_gem_object_change_domain(obj, old_read, old_write);
        }
  }
  
- static void
- i915_gem_execbuffer_retire_commands(struct i915_execbuffer_params *params)
- {
-       /* Unconditionally force add_request to emit a full flush. */
-       params->engine->gpu_caches_dirty = true;
-       /* Add a breadcrumb for the completion of the batch buffer */
-       __i915_add_request(params->request, params->batch_obj, true);
- }
  static int
- i915_reset_gen7_sol_offsets(struct drm_device *dev,
-                           struct drm_i915_gem_request *req)
+ i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
  {
-       struct intel_engine_cs *engine = req->engine;
-       struct drm_i915_private *dev_priv = to_i915(dev);
+       struct intel_ring *ring = req->ring;
        int ret, i;
  
-       if (!IS_GEN7(dev) || engine != &dev_priv->engine[RCS]) {
+       if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
                DRM_DEBUG("sol reset is gen7/rcs only\n");
                return -EINVAL;
        }
                return ret;
  
        for (i = 0; i < 4; i++) {
-               intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
-               intel_ring_emit_reg(engine, GEN7_SO_WRITE_OFFSET(i));
-               intel_ring_emit(engine, 0);
+               intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
+               intel_ring_emit_reg(ring, GEN7_SO_WRITE_OFFSET(i));
+               intel_ring_emit(ring, 0);
        }
  
-       intel_ring_advance(engine);
+       intel_ring_advance(ring);
  
        return 0;
  }
  
- static struct drm_i915_gem_object*
+ static struct i915_vma *
  i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
                          struct drm_i915_gem_exec_object2 *shadow_exec_entry,
-                         struct eb_vmas *eb,
                          struct drm_i915_gem_object *batch_obj,
+                         struct eb_vmas *eb,
                          u32 batch_start_offset,
                          u32 batch_len,
                          bool is_master)
        shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
                                                   PAGE_ALIGN(batch_len));
        if (IS_ERR(shadow_batch_obj))
-               return shadow_batch_obj;
-       ret = i915_parse_cmds(engine,
-                             batch_obj,
-                             shadow_batch_obj,
-                             batch_start_offset,
-                             batch_len,
-                             is_master);
-       if (ret)
-               goto err;
-       ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
-       if (ret)
-               goto err;
+               return ERR_CAST(shadow_batch_obj);
+       ret = intel_engine_cmd_parser(engine,
+                                     batch_obj,
+                                     shadow_batch_obj,
+                                     batch_start_offset,
+                                     batch_len,
+                                     is_master);
+       if (ret) {
+               if (ret == -EACCES) /* unhandled chained batch */
+                       vma = NULL;
+               else
+                       vma = ERR_PTR(ret);
+               goto out;
+       }
  
-       i915_gem_object_unpin_pages(shadow_batch_obj);
+       vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
+       if (IS_ERR(vma))
+               goto out;
  
        memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
  
-       vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
        vma->exec_entry = shadow_exec_entry;
        vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
-       drm_gem_object_reference(&shadow_batch_obj->base);
+       i915_gem_object_get(shadow_batch_obj);
        list_add_tail(&vma->exec_list, &eb->vmas);
  
-       shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND;
-       return shadow_batch_obj;
- err:
+ out:
        i915_gem_object_unpin_pages(shadow_batch_obj);
-       if (ret == -EACCES) /* unhandled chained batch */
-               return batch_obj;
-       else
-               return ERR_PTR(ret);
+       return vma;
  }
  
- int
i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params,
-                              struct drm_i915_gem_execbuffer2 *args,
-                              struct list_head *vmas)
static int
execbuf_submit(struct i915_execbuffer_params *params,
+              struct drm_i915_gem_execbuffer2 *args,
+              struct list_head *vmas)
  {
-       struct drm_device *dev = params->dev;
-       struct intel_engine_cs *engine = params->engine;
-       struct drm_i915_private *dev_priv = to_i915(dev);
+       struct drm_i915_private *dev_priv = params->request->i915;
        u64 exec_start, exec_len;
        int instp_mode;
        u32 instp_mask;
        if (ret)
                return ret;
  
-       WARN(params->ctx->ppgtt && params->ctx->ppgtt->pd_dirty_rings & (1<<engine->id),
-            "%s didn't clear reload\n", engine->name);
        instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
        instp_mask = I915_EXEC_CONSTANTS_MASK;
        switch (instp_mode) {
        case I915_EXEC_CONSTANTS_REL_GENERAL:
        case I915_EXEC_CONSTANTS_ABSOLUTE:
        case I915_EXEC_CONSTANTS_REL_SURFACE:
-               if (instp_mode != 0 && engine != &dev_priv->engine[RCS]) {
+               if (instp_mode != 0 && params->engine->id != RCS) {
                        DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
                        return -EINVAL;
                }
  
                if (instp_mode != dev_priv->relative_constants_mode) {
-                       if (INTEL_INFO(dev)->gen < 4) {
+                       if (INTEL_INFO(dev_priv)->gen < 4) {
                                DRM_DEBUG("no rel constants on pre-gen4\n");
                                return -EINVAL;
                        }
  
-                       if (INTEL_INFO(dev)->gen > 5 &&
+                       if (INTEL_INFO(dev_priv)->gen > 5 &&
                            instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
                                DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
                                return -EINVAL;
                        }
  
                        /* The HW changed the meaning on this bit on gen6 */
-                       if (INTEL_INFO(dev)->gen >= 6)
+                       if (INTEL_INFO(dev_priv)->gen >= 6)
                                instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
                }
                break;
                return -EINVAL;
        }
  
-       if (engine == &dev_priv->engine[RCS] &&
+       if (params->engine->id == RCS &&
            instp_mode != dev_priv->relative_constants_mode) {
+               struct intel_ring *ring = params->request->ring;
                ret = intel_ring_begin(params->request, 4);
                if (ret)
                        return ret;
  
-               intel_ring_emit(engine, MI_NOOP);
-               intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
-               intel_ring_emit_reg(engine, INSTPM);
-               intel_ring_emit(engine, instp_mask << 16 | instp_mode);
-               intel_ring_advance(engine);
+               intel_ring_emit(ring, MI_NOOP);
+               intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
+               intel_ring_emit_reg(ring, INSTPM);
+               intel_ring_emit(ring, instp_mask << 16 | instp_mode);
+               intel_ring_advance(ring);
  
                dev_priv->relative_constants_mode = instp_mode;
        }
  
        if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
-               ret = i915_reset_gen7_sol_offsets(dev, params->request);
+               ret = i915_reset_gen7_sol_offsets(params->request);
                if (ret)
                        return ret;
        }
  
        exec_len   = args->batch_len;
-       exec_start = params->batch_obj_vm_offset +
+       exec_start = params->batch->node.start +
                     params->args_batch_start_offset;
  
        if (exec_len == 0)
-               exec_len = params->batch_obj->base.size;
+               exec_len = params->batch->size - params->args_batch_start_offset;
  
-       ret = engine->dispatch_execbuffer(params->request,
-                                       exec_start, exec_len,
-                                       params->dispatch_flags);
+       ret = params->engine->emit_bb_start(params->request,
+                                           exec_start, exec_len,
+                                           params->dispatch_flags);
        if (ret)
                return ret;
  
  
  /**
   * Find one BSD ring to dispatch the corresponding BSD command.
-  * The ring index is returned.
+  * The engine index is returned.
   */
  static unsigned int
- gen8_dispatch_bsd_ring(struct drm_i915_private *dev_priv, struct drm_file *file)
+ gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
+                        struct drm_file *file)
  {
        struct drm_i915_file_private *file_priv = file->driver_priv;
  
        /* Check whether the file_priv has already selected one ring. */
-       if ((int)file_priv->bsd_ring < 0) {
-               /* If not, use the ping-pong mechanism to select one. */
-               mutex_lock(&dev_priv->drm.struct_mutex);
-               file_priv->bsd_ring = dev_priv->mm.bsd_ring_dispatch_index;
-               dev_priv->mm.bsd_ring_dispatch_index ^= 1;
-               mutex_unlock(&dev_priv->drm.struct_mutex);
-       }
-       return file_priv->bsd_ring;
- }
- static struct drm_i915_gem_object *
- eb_get_batch(struct eb_vmas *eb)
- {
-       struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
-       /*
-        * SNA is doing fancy tricks with compressing batch buffers, which leads
-        * to negative relocation deltas. Usually that works out ok since the
-        * relocate address is still positive, except when the batch is placed
-        * very low in the GTT. Ensure this doesn't happen.
-        *
-        * Note that actual hangs have only been observed on gen7, but for
-        * paranoia do it everywhere.
-        */
-       if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
-               vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
+       if ((int)file_priv->bsd_engine < 0)
+               file_priv->bsd_engine = atomic_fetch_xor(1,
+                        &dev_priv->mm.bsd_engine_dispatch_index);
  
-       return vma->obj;
+       return file_priv->bsd_engine;
  }
  
  #define I915_USER_RINGS (4)
@@@ -1360,31 -1564,31 +1564,31 @@@ static const enum intel_engine_id user_
        [I915_EXEC_VEBOX]       = VECS
  };
  
- static int
- eb_select_ring(struct drm_i915_private *dev_priv,
-              struct drm_file *file,
-              struct drm_i915_gem_execbuffer2 *args,
-              struct intel_engine_cs **ring)
+ static struct intel_engine_cs *
+ eb_select_engine(struct drm_i915_private *dev_priv,
+                struct drm_file *file,
+                struct drm_i915_gem_execbuffer2 *args)
  {
        unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
+       struct intel_engine_cs *engine;
  
        if (user_ring_id > I915_USER_RINGS) {
                DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
-               return -EINVAL;
+               return NULL;
        }
  
        if ((user_ring_id != I915_EXEC_BSD) &&
            ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
                DRM_DEBUG("execbuf with non bsd ring but with invalid "
                          "bsd dispatch flags: %d\n", (int)(args->flags));
-               return -EINVAL;
+               return NULL;
        }
  
        if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
                unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
  
                if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
-                       bsd_idx = gen8_dispatch_bsd_ring(dev_priv, file);
+                       bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
                } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
                           bsd_idx <= I915_EXEC_BSD_RING2) {
                        bsd_idx >>= I915_EXEC_BSD_SHIFT;
                } else {
                        DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
                                  bsd_idx);
-                       return -EINVAL;
+                       return NULL;
                }
  
-               *ring = &dev_priv->engine[_VCS(bsd_idx)];
+               engine = &dev_priv->engine[_VCS(bsd_idx)];
        } else {
-               *ring = &dev_priv->engine[user_ring_map[user_ring_id]];
+               engine = &dev_priv->engine[user_ring_map[user_ring_id]];
        }
  
-       if (!intel_engine_initialized(*ring)) {
+       if (!intel_engine_initialized(engine)) {
                DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
-               return -EINVAL;
+               return NULL;
        }
  
-       return 0;
+       return engine;
  }
  
  static int
@@@ -1416,9 -1620,7 +1620,7 @@@ i915_gem_do_execbuffer(struct drm_devic
  {
        struct drm_i915_private *dev_priv = to_i915(dev);
        struct i915_ggtt *ggtt = &dev_priv->ggtt;
-       struct drm_i915_gem_request *req = NULL;
        struct eb_vmas *eb;
-       struct drm_i915_gem_object *batch_obj;
        struct drm_i915_gem_exec_object2 shadow_exec_entry;
        struct intel_engine_cs *engine;
        struct i915_gem_context *ctx;
        if (args->flags & I915_EXEC_IS_PINNED)
                dispatch_flags |= I915_DISPATCH_PINNED;
  
-       ret = eb_select_ring(dev_priv, file, args, &engine);
-       if (ret)
-               return ret;
+       engine = eb_select_engine(dev_priv, file, args);
+       if (!engine)
+               return -EINVAL;
  
        if (args->buffer_count < 1) {
                DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
                goto pre_mutex_err;
        }
  
-       i915_gem_context_reference(ctx);
+       i915_gem_context_get(ctx);
  
        if (ctx->ppgtt)
                vm = &ctx->ppgtt->base;
  
        memset(&params_master, 0x00, sizeof(params_master));
  
-       eb = eb_create(args);
+       eb = eb_create(dev_priv, args);
        if (eb == NULL) {
-               i915_gem_context_unreference(ctx);
+               i915_gem_context_put(ctx);
                mutex_unlock(&dev->struct_mutex);
                ret = -ENOMEM;
                goto pre_mutex_err;
                goto err;
  
        /* take note of the batch buffer before we might reorder the lists */
-       batch_obj = eb_get_batch(eb);
+       params->batch = eb_get_batch(eb);
  
        /* Move the objects en-masse into the GTT, evicting if necessary. */
        need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
        }
  
        /* Set the pending read domains for the batch buffer to COMMAND */
-       if (batch_obj->base.pending_write_domain) {
+       if (params->batch->obj->base.pending_write_domain) {
                DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
                ret = -EINVAL;
                goto err;
        }
+       if (args->batch_start_offset > params->batch->size ||
+           args->batch_len > params->batch->size - args->batch_start_offset) {
+               DRM_DEBUG("Attempting to use out-of-bounds batch\n");
+               ret = -EINVAL;
+               goto err;
+       }
  
        params->args_batch_start_offset = args->batch_start_offset;
-       if (i915_needs_cmd_parser(engine) && args->batch_len) {
-               struct drm_i915_gem_object *parsed_batch_obj;
-               parsed_batch_obj = i915_gem_execbuffer_parse(engine,
-                                                            &shadow_exec_entry,
-                                                            eb,
-                                                            batch_obj,
-                                                            args->batch_start_offset,
-                                                            args->batch_len,
-                                                            drm_is_current_master(file));
-               if (IS_ERR(parsed_batch_obj)) {
-                       ret = PTR_ERR(parsed_batch_obj);
+       if (intel_engine_needs_cmd_parser(engine) && args->batch_len) {
+               struct i915_vma *vma;
+               vma = i915_gem_execbuffer_parse(engine, &shadow_exec_entry,
+                                               params->batch->obj,
+                                               eb,
+                                               args->batch_start_offset,
+                                               args->batch_len,
+                                               drm_is_current_master(file));
+               if (IS_ERR(vma)) {
+                       ret = PTR_ERR(vma);
                        goto err;
                }
  
-               /*
-                * parsed_batch_obj == batch_obj means batch not fully parsed:
-                * Accept, but don't promote to secure.
-                */
-               if (parsed_batch_obj != batch_obj) {
+               if (vma) {
                        /*
                         * Batch parsed and accepted:
                         *
                         */
                        dispatch_flags |= I915_DISPATCH_SECURE;
                        params->args_batch_start_offset = 0;
-                       batch_obj = parsed_batch_obj;
+                       params->batch = vma;
                }
        }
  
-       batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
+       params->batch->obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  
        /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
         * batch" bit. Hence we need to pin secure batches into the global gtt.
         * hsw should have this fixed, but bdw mucks it up again. */
        if (dispatch_flags & I915_DISPATCH_SECURE) {
+               struct drm_i915_gem_object *obj = params->batch->obj;
+               struct i915_vma *vma;
                /*
                 * So on first glance it looks freaky that we pin the batch here
                 * outside of the reservation loop. But:
                 *   fitting due to fragmentation.
                 * So this is actually safe.
                 */
-               ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
-               if (ret)
+               vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
+               if (IS_ERR(vma)) {
+                       ret = PTR_ERR(vma);
                        goto err;
+               }
  
-               params->batch_obj_vm_offset = i915_gem_obj_ggtt_offset(batch_obj);
-       } else
-               params->batch_obj_vm_offset = i915_gem_obj_offset(batch_obj, vm);
+               params->batch = vma;
+       }
  
        /* Allocate a request for this batch buffer nice and early. */
-       req = i915_gem_request_alloc(engine, ctx);
-       if (IS_ERR(req)) {
-               ret = PTR_ERR(req);
+       params->request = i915_gem_request_alloc(engine, ctx);
+       if (IS_ERR(params->request)) {
+               ret = PTR_ERR(params->request);
                goto err_batch_unpin;
        }
  
-       ret = i915_gem_request_add_to_client(req, file);
+       /* Whilst this request exists, batch_obj will be on the
+        * active_list, and so will hold the active reference. Only when this
+        * request is retired will the the batch_obj be moved onto the
+        * inactive_list and lose its active reference. Hence we do not need
+        * to explicitly hold another reference here.
+        */
+       params->request->batch = params->batch;
+       ret = i915_gem_request_add_to_client(params->request, file);
        if (ret)
                goto err_request;
  
        params->file                    = file;
        params->engine                    = engine;
        params->dispatch_flags          = dispatch_flags;
-       params->batch_obj               = batch_obj;
        params->ctx                     = ctx;
-       params->request                 = req;
  
-       ret = dev_priv->gt.execbuf_submit(params, args, &eb->vmas);
+       ret = execbuf_submit(params, args, &eb->vmas);
  err_request:
-       i915_gem_execbuffer_retire_commands(params);
+       __i915_add_request(params->request, ret == 0);
  
  err_batch_unpin:
        /*
         * active.
         */
        if (dispatch_flags & I915_DISPATCH_SECURE)
-               i915_gem_object_ggtt_unpin(batch_obj);
+               i915_vma_unpin(params->batch);
  err:
        /* the request owns the ref now */
-       i915_gem_context_unreference(ctx);
+       i915_gem_context_put(ctx);
        eb_destroy(eb);
  
        mutex_unlock(&dev->struct_mutex);
@@@ -209,8 -209,7 +209,8 @@@ nouveau_bo_new(struct drm_device *dev, 
        nvbo->tile_flags = tile_flags;
        nvbo->bo.bdev = &drm->ttm.bdev;
  
 -      nvbo->force_coherent = flags & TTM_PL_FLAG_UNCACHED;
 +      if (!nvxx_device(&drm->device)->func->cpu_coherent)
 +              nvbo->force_coherent = flags & TTM_PL_FLAG_UNCACHED;
  
        nvbo->page_shift = 12;
        if (drm->client.vm) {
@@@ -1152,7 -1151,7 +1152,7 @@@ nouveau_bo_move_flipd(struct ttm_buffer
        if (ret)
                goto out;
  
-       ret = ttm_bo_move_ttm(bo, true, intr, no_wait_gpu, new_mem);
+       ret = ttm_bo_move_ttm(bo, intr, no_wait_gpu, new_mem);
  out:
        ttm_bo_mem_put(bo, &tmp_mem);
        return ret;
@@@ -1180,7 -1179,7 +1180,7 @@@ nouveau_bo_move_flips(struct ttm_buffer
        if (ret)
                return ret;
  
-       ret = ttm_bo_move_ttm(bo, true, intr, no_wait_gpu, &tmp_mem);
+       ret = ttm_bo_move_ttm(bo, intr, no_wait_gpu, &tmp_mem);
        if (ret)
                goto out;
  
@@@ -1298,7 -1297,7 +1298,7 @@@ nouveau_bo_move(struct ttm_buffer_objec
        /* Fallback to software copy. */
        ret = ttm_bo_wait(bo, intr, no_wait_gpu);
        if (ret == 0)
-               ret = ttm_bo_move_memcpy(bo, evict, intr, no_wait_gpu, new_mem);
+               ret = ttm_bo_move_memcpy(bo, intr, no_wait_gpu, new_mem);
  
  out:
        if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
@@@ -1316,7 -1315,8 +1316,8 @@@ nouveau_bo_verify_access(struct ttm_buf
  {
        struct nouveau_bo *nvbo = nouveau_bo(bo);
  
-       return drm_vma_node_verify_access(&nvbo->gem.vma_node, filp);
+       return drm_vma_node_verify_access(&nvbo->gem.vma_node,
+                                         filp->private_data);
  }
  
  static int
@@@ -3015,12 -3015,6 +3015,12 @@@ static void si_apply_state_adjust_rules
        if (rdev->pdev->device == 0x6811 &&
            rdev->pdev->revision == 0x81)
                max_mclk = 120000;
 +      /* limit sclk/mclk on Jet parts for stability */
 +      if (rdev->pdev->device == 0x6665 &&
 +          rdev->pdev->revision == 0xc3) {
 +              max_sclk = 75000;
 +              max_mclk = 80000;
 +      }
  
        if (rps->vce_active) {
                rps->evclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].evclk;
@@@ -4112,7 -4106,7 +4112,7 @@@ static int si_populate_smc_voltage_tabl
                                                              &rdev->pm.dpm.dyn_state.phase_shedding_limits_table)) {
                                si_populate_smc_voltage_table(rdev, &si_pi->vddc_phase_shed_table, table);
  
-                               table->phaseMaskTable.lowMask[SISLANDS_SMC_VOLTAGEMASK_VDDC] =
+                               table->phaseMaskTable.lowMask[SISLANDS_SMC_VOLTAGEMASK_VDDC_PHASE_SHEDDING] =
                                        cpu_to_be32(si_pi->vddc_phase_shed_table.mask_low);
  
                                si_write_smc_soft_register(rdev, SI_SMC_SOFT_REGISTER_phase_shedding_delay,
@@@ -71,7 -71,6 +71,7 @@@ header-y += binfmts.
  header-y += blkpg.h
  header-y += blktrace_api.h
  header-y += bpf_common.h
 +header-y += bpf_perf_event.h
  header-y += bpf.h
  header-y += bpqether.h
  header-y += bsg.h
@@@ -336,8 -335,6 +336,8 @@@ header-y += pkt_cls.
  header-y += pkt_sched.h
  header-y += pmu.h
  header-y += poll.h
 +header-y += posix_acl.h
 +header-y += posix_acl_xattr.h
  header-y += posix_types.h
  header-y += ppdev.h
  header-y += ppp-comp.h
@@@ -399,6 -396,7 +399,7 @@@ header-y += string.
  header-y += suspend_ioctls.h
  header-y += swab.h
  header-y += synclink.h
+ header-y += sync_file.h
  header-y += sysctl.h
  header-y += sysinfo.h
  header-y += target_core_user.h