drm: Add shmem GEM library

Submitted by Noralf Trønnes on July 12, 2017, 1:45 p.m.

Details

Reviewer None
Submitted July 12, 2017, 1:45 p.m.
Last Updated Oct. 17, 2018, 1:05 p.m.
Revision 6

Cover Letter(s)

Revision 1
      This patchset adds a library for shmem backed GEM objects.
I'm putting out an rfc to find out if I'm on the right track with this.
The shmem library is very similar to the cma library, so I have tried
to pull out the common bits.

A couple of questions:
- What should the default cache mode be?
- Do I need locking around vmap/vunmap?
  See TODO in [RFC 5/7] drm: Add library for shmem backed GEM objects

Noralf.


Noralf Trønnes (7):
  drm/gem: Add drm_gem_dumb_map_offset()
  drm: Add GEM backed framebuffer library
  drm/fb-helper: Support shadow buffer with deferred io
  drm/fb-helper: Add simple init/fini functions
  drm: Add library for shmem backed GEM objects
  drm: Add kms library for shmem backed GEM
  drm/tinydrm: Switch from CMA to shmem buffers

 drivers/gpu/drm/Kconfig                     |  17 +
 drivers/gpu/drm/Makefile                    |   4 +-
 drivers/gpu/drm/drm_fb_gem_helper.c         | 248 +++++++++++
 drivers/gpu/drm/drm_fb_helper.c             | 229 +++++++++-
 drivers/gpu/drm/drm_fb_shmem_helper.c       | 168 ++++++++
 drivers/gpu/drm/drm_gem.c                   |  35 ++
 drivers/gpu/drm/drm_gem_shmem_helper.c      | 628 ++++++++++++++++++++++++++++
 drivers/gpu/drm/tinydrm/Kconfig             |   2 +-
 drivers/gpu/drm/tinydrm/core/tinydrm-core.c | 147 +++----
 drivers/gpu/drm/tinydrm/core/tinydrm-pipe.c |   4 +-
 drivers/gpu/drm/tinydrm/mi0283qt.c          |   2 +-
 drivers/gpu/drm/tinydrm/mipi-dbi.c          |  41 +-
 include/drm/drm_fb_gem_helper.h             |  64 +++
 include/drm/drm_fb_helper.h                 |  50 +++
 include/drm/drm_fb_shmem_helper.h           |  18 +
 include/drm/drm_gem.h                       |   2 +
 include/drm/drm_gem_shmem_helper.h          | 131 ++++++
 include/drm/tinydrm/tinydrm.h               |  32 +-
 18 files changed, 1683 insertions(+), 139 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_fb_gem_helper.c
 create mode 100644 drivers/gpu/drm/drm_fb_shmem_helper.c
 create mode 100644 drivers/gpu/drm/drm_gem_shmem_helper.c
 create mode 100644 include/drm/drm_fb_gem_helper.h
 create mode 100644 include/drm/drm_fb_shmem_helper.h
 create mode 100644 include/drm/drm_gem_shmem_helper.h

--
2.7.4
    
Revision 2
      This patchset adds a library for shmem backed GEM objects and makes use
of it in tinydrm.

When I made tinydrm I used the CMA helper because it was very easy to
use. July last year I learned that this limits which drivers to PRIME
import from, since CMA requires continuous memory. tinydrm drivers don't
require that. So I set out to change that looking first at shmem, but
that wasn't working since shmem didn't work with fbdev deferred I/O.
Then I did a vmalloc buffer attempt which worked with deferred I/O, but
maybe wouldn't be of so much use as a library for other drivers to use.
As my work to split out stuff from the CMA helper for shared use came to
an end, I had a generic fbdev emulation that uses a shadow buffer for
deferred I/O.
This means that I can now use shmem buffers after all.

I have looked at the other drivers that use drm_gem_get_pages() and
several supports different cache modes so I've done that even though
tinydrm only uses the cached one.

tinydrm can both use vmalloc and shmem buffers, it doesn't matter as far
as I can see. So the question is what will benefit the rest of DRM the
most.

This is what a vmalloc helper might look like (minus the now unneeded
fbdev stuff): https://patchwork.freedesktop.org/patch/182735/

Noralf.

Noralf Trønnes (2):
  drm: Add library for shmem backed GEM objects
  drm/tinydrm: Switch from CMA to shmem buffers

 Documentation/gpu/drm-kms-helpers.rst          |  12 +
 drivers/gpu/drm/Kconfig                        |   6 +
 drivers/gpu/drm/Makefile                       |   1 +
 drivers/gpu/drm/drm_gem_shmem_helper.c         | 673 +++++++++++++++++++++++++
 drivers/gpu/drm/tinydrm/Kconfig                |   2 +-
 drivers/gpu/drm/tinydrm/core/tinydrm-core.c    |  90 +---
 drivers/gpu/drm/tinydrm/core/tinydrm-helpers.c |   5 +
 drivers/gpu/drm/tinydrm/ili9225.c              |  14 +-
 drivers/gpu/drm/tinydrm/ili9341.c              |   6 +-
 drivers/gpu/drm/tinydrm/mi0283qt.c             |   6 +-
 drivers/gpu/drm/tinydrm/mipi-dbi.c             |  38 +-
 drivers/gpu/drm/tinydrm/repaper.c              |  24 +-
 drivers/gpu/drm/tinydrm/st7586.c               |  15 +-
 drivers/gpu/drm/tinydrm/st7735r.c              |   6 +-
 include/drm/drm_gem_shmem_helper.h             | 198 ++++++++
 include/drm/tinydrm/tinydrm.h                  |  36 +-
 16 files changed, 978 insertions(+), 154 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_gem_shmem_helper.c
 create mode 100644 include/drm/drm_gem_shmem_helper.h
    
Revision 3
      This patchset adds a library for shmem backed GEM objects and makes use
of it in tinydrm.

When I made tinydrm I used the CMA helper because it was very easy to
use. July last year I learned that this limits which drivers to PRIME
import from, since CMA requires continuous memory. tinydrm drivers don't
require that. So I set out to change that looking first at shmem, but
that wasn't working since shmem didn't work with fbdev deferred I/O.
Then I did a vmalloc buffer attempt which worked with deferred I/O, but
maybe wouldn't be of so much use as a library for other drivers to use.
As my work to split out stuff from the CMA helper for shared use came to
an end, I had a generic fbdev emulation that uses a shadow buffer for
deferred I/O.
This means that I can now use shmem buffers after all.

I have looked at the other drivers that use drm_gem_get_pages() and
several supports different cache modes so I've done that even though
tinydrm only uses the cached one.

tinydrm can both use vmalloc and shmem buffers, it doesn't matter as far
as I can see. So the question is what will benefit the rest of DRM the
most.

Note:
Sparse has this complaint, but the problem is in kvmalloc_array():
include/linux/mm.h:592:13: error: undefined identifier '__builtin_mul_overflow'

Noralf.

Changes since version 1:
- Fix missing argument in docs (kbuild test robot)
- Fix: sparse: expression using sizeof(void) (kbuild test robot)
- Rebasing gave a new checkpatch warning, so I changed to bitfields:
  CHECK: Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384
  #834: FILE: include/drm/drm_gem_shmem_helper.h:84:
  +       bool pages_mark_dirty_on_put;
  #841: FILE: include/drm/drm_gem_shmem_helper.h:91:
  +       bool pages_mark_accessed_on_put;

Noralf Trønnes (2):
  drm: Add library for shmem backed GEM objects
  drm/tinydrm: Switch from CMA to shmem buffers

 Documentation/gpu/drm-kms-helpers.rst          |  12 +
 drivers/gpu/drm/Kconfig                        |   6 +
 drivers/gpu/drm/Makefile                       |   1 +
 drivers/gpu/drm/drm_gem_shmem_helper.c         | 676 +++++++++++++++++++++++++
 drivers/gpu/drm/tinydrm/Kconfig                |   2 +-
 drivers/gpu/drm/tinydrm/core/tinydrm-core.c    |  91 +---
 drivers/gpu/drm/tinydrm/core/tinydrm-helpers.c |   5 +
 drivers/gpu/drm/tinydrm/ili9225.c              |  14 +-
 drivers/gpu/drm/tinydrm/ili9341.c              |   6 +-
 drivers/gpu/drm/tinydrm/mi0283qt.c             |   6 +-
 drivers/gpu/drm/tinydrm/mipi-dbi.c             |  38 +-
 drivers/gpu/drm/tinydrm/repaper.c              |  24 +-
 drivers/gpu/drm/tinydrm/st7586.c               |  15 +-
 drivers/gpu/drm/tinydrm/st7735r.c              |   6 +-
 include/drm/drm_gem_shmem_helper.h             | 198 ++++++++
 include/drm/tinydrm/tinydrm.h                  |  36 +-
 16 files changed, 982 insertions(+), 154 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_gem_shmem_helper.c
 create mode 100644 include/drm/drm_gem_shmem_helper.h
    
Revision 4
      This patchset adds a library for shmem backed GEM objects and makes use
of it in tinydrm.

When I made tinydrm I used the CMA helper because it was very easy to
use. July last year I learned that this limits which drivers to PRIME
import from, since CMA requires continuous memory. tinydrm drivers don't
require that. So I set out to change that looking first at shmem, but
that wasn't working since shmem didn't work with fbdev deferred I/O.
Then I did a vmalloc buffer attempt which worked with deferred I/O, but
maybe wouldn't be of so much use as a library for other drivers to use.
As my work to split out stuff from the CMA helper for shared use came to
an end, I had a generic fbdev emulation that uses a shadow buffer for
deferred I/O.
This means that I can now use shmem buffers after all.

I have looked at the other drivers that use drm_gem_get_pages() and
several supports different cache modes so I've done that even though
tinydrm only uses the cached one.

tinydrm can both use vmalloc and shmem buffers, it doesn't matter as far
as I can see. So the question is what will benefit the rest of DRM the most.

Note:
Sparse has this complaint, but the problem is in kvmalloc_array():
include/linux/mm.h:592:13: error: undefined identifier '__builtin_mul_overflow'

Noralf.

Changes since version 2:
- Grammar (Sam Ravnborg)
- s/drm_gem_shmem_put_pages_unlocked/drm_gem_shmem_put_pages_locked/
  (Sam Ravnborg)
- Add debug ouput in error path (Sam Ravnborg)

Changes since version 1:
- Fix missing argument in docs (kbuild test robot)
- Fix: sparse: expression using sizeof(void) (kbuild test robot)
- Rebasing gave a new checkpatch warning, so I changed to bitfields:
  CHECK: Avoid using bool structure members because of possible alignment
  issues - see: https://lkml.org/lkml/2017/11/21/384
  #834: FILE: include/drm/drm_gem_shmem_helper.h:84:
  +       bool pages_mark_dirty_on_put;
  #841: FILE: include/drm/drm_gem_shmem_helper.h:91:
  +       bool pages_mark_accessed_on_put;

Noralf Trønnes (2):
  drm: Add library for shmem backed GEM objects
  drm/tinydrm: Switch from CMA to shmem buffers

 Documentation/gpu/drm-kms-helpers.rst          |  12 +
 drivers/gpu/drm/Kconfig                        |   6 +
 drivers/gpu/drm/Makefile                       |   1 +
 drivers/gpu/drm/drm_gem_shmem_helper.c         | 678 +++++++++++++++++++++++++
 drivers/gpu/drm/tinydrm/Kconfig                |   2 +-
 drivers/gpu/drm/tinydrm/core/tinydrm-core.c    |  91 +---
 drivers/gpu/drm/tinydrm/core/tinydrm-helpers.c |   5 +
 drivers/gpu/drm/tinydrm/ili9225.c              |  14 +-
 drivers/gpu/drm/tinydrm/ili9341.c              |   6 +-
 drivers/gpu/drm/tinydrm/mi0283qt.c             |   6 +-
 drivers/gpu/drm/tinydrm/mipi-dbi.c             |  38 +-
 drivers/gpu/drm/tinydrm/repaper.c              |  24 +-
 drivers/gpu/drm/tinydrm/st7586.c               |  15 +-
 drivers/gpu/drm/tinydrm/st7735r.c              |   6 +-
 include/drm/drm_gem_shmem_helper.h             | 198 ++++++++
 include/drm/tinydrm/tinydrm.h                  |  36 +-
 16 files changed, 984 insertions(+), 154 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_gem_shmem_helper.c
 create mode 100644 include/drm/drm_gem_shmem_helper.h
    
Revision 5
      This patchset adds a library for shmem backed GEM objects and makes use
of it in tinydrm.

Thomas gave me some feedback that made me realise that the cachemodes
pattern I had picked up from some other drivers didn't apply to shmem
buffers in the way I though it did. So it's removed.

I recently did an RFC to see if it made sense to add a vtable to GEM
objects. Daniel welcomed that so I've included it here.

Noralf.

Changes since version 3:
- Drop cache modes (Thomas Hellstrom)
- Add a GEM object attached vtable

Changes since version 2:
- Grammar (Sam Ravnborg)
- s/drm_gem_shmem_put_pages_unlocked/drm_gem_shmem_put_pages_locked/
(Sam Ravnborg)
- Add debug ouput in error path (Sam Ravnborg)

Changes since version 1:
- Fix missing argument in docs (kbuild test robot)
- Fix: sparse: expression using sizeof(void) (kbuild test robot)
- Rebasing gave a new checkpatch warning, so I changed to bitfields:
CHECK: Avoid using bool structure members because of possible alignment
issues - see: https://lkml.org/lkml/2017/11/21/384
#834: FILE: include/drm/drm_gem_shmem_helper.h:84:
+       bool pages_mark_dirty_on_put;
#841: FILE: include/drm/drm_gem_shmem_helper.h:91:
+       bool pages_mark_accessed_on_put;

Noralf Trønnes (4):
  drm/driver: Add defaults for .gem_prime_export/import callbacks
  drm/gem: Add drm_gem_object_funcs
  drm: Add library for shmem backed GEM objects
  drm/tinydrm: Switch from CMA to shmem buffers

 Documentation/gpu/drm-kms-helpers.rst          |  12 +
 Documentation/gpu/todo.rst                     |   7 +
 drivers/gpu/drm/Kconfig                        |   6 +
 drivers/gpu/drm/Makefile                       |   1 +
 drivers/gpu/drm/drm_client.c                   |  12 +-
 drivers/gpu/drm/drm_fb_helper.c                |   8 +-
 drivers/gpu/drm/drm_gem.c                      | 109 ++++-
 drivers/gpu/drm/drm_gem_shmem_helper.c         | 586 +++++++++++++++++++++++++
 drivers/gpu/drm/drm_prime.c                    |  50 ++-
 drivers/gpu/drm/tinydrm/Kconfig                |   2 +-
 drivers/gpu/drm/tinydrm/core/tinydrm-core.c    |  92 ++--
 drivers/gpu/drm/tinydrm/core/tinydrm-helpers.c |   5 +
 drivers/gpu/drm/tinydrm/ili9225.c              |  14 +-
 drivers/gpu/drm/tinydrm/ili9341.c              |   6 +-
 drivers/gpu/drm/tinydrm/mi0283qt.c             |   6 +-
 drivers/gpu/drm/tinydrm/mipi-dbi.c             |  38 +-
 drivers/gpu/drm/tinydrm/repaper.c              |  24 +-
 drivers/gpu/drm/tinydrm/st7586.c               |  15 +-
 drivers/gpu/drm/tinydrm/st7735r.c              |   6 +-
 include/drm/drm_drv.h                          |   4 +
 include/drm/drm_gem.h                          | 140 ++++++
 include/drm/drm_gem_shmem_helper.h             | 153 +++++++
 include/drm/tinydrm/tinydrm.h                  |  36 +-
 23 files changed, 1139 insertions(+), 193 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_gem_shmem_helper.c
 create mode 100644 include/drm/drm_gem_shmem_helper.h
    
Revision 6
      This patchset adds a library for shmem backed GEM objects and makes use
of it in tinydrm.

Daniel suggested that I make a generic mmap function for GEM PRIME.
I wondered if I could make it the default for drm_driver->gem_prime_mmap
so I looked at all the drivers that don't set that callback:
- armada, i915, omap and udl has their own dmabuf implementations.
- nouveau and radeon use drm_gem_prime_export(), but don't set the
  ->gem_prime_mmap callback.

Making it default would change the behaviour of those last two so I let
it be.

I'm cc'ing intel-gfx to have the CI look at the core changes I've made.
I can't exercise all the codepaths using vc4 and tinydrm.

Noralf.

Changes since version 4:
- Add drm_gem_prime_mmap() (Daniel Vetter)
- Drop drm_gem_object_funcs->prime_mmap
- drm_gem_shmem_mmap(): Subtract drm_vma_node_start() to get the real
  vma->vm_pgoff
- drm_gem_shmem_fault(): Use vmf->pgoff now that vma->vm_pgoff is
  correct

Changes since version 3:
- Drop cache modes (Thomas Hellstrom)
- Add a GEM object attached vtable

Changes since version 2:
- Grammar (Sam Ravnborg)
- s/drm_gem_shmem_put_pages_unlocked/drm_gem_shmem_put_pages_locked/
  (Sam Ravnborg)
- Add debug ouput in error path (Sam Ravnborg)

Changes since version 1:
- Fix missing argument in docs (kbuild test robot)
- Fix: sparse: expression using sizeof(void) (kbuild test robot)
- Rebasing gave a new checkpatch warning, so I changed to bitfields:
CHECK: Avoid using bool structure members because of possible alignment
issues - see: https://lkml.org/lkml/2017/11/21/384
#834: FILE: include/drm/drm_gem_shmem_helper.h:84:
+       bool pages_mark_dirty_on_put;
#841: FILE: include/drm/drm_gem_shmem_helper.h:91:
+       bool pages_mark_accessed_on_put;

Noralf Trønnes (5):
  drm/driver: Add defaults for .gem_prime_export/import callbacks
  drm/prime: Add drm_gem_prime_mmap()
  drm/gem: Add drm_gem_object_funcs
  drm: Add library for shmem backed GEM objects
  drm/tinydrm: Switch from CMA to shmem buffers

 Documentation/gpu/drm-kms-helpers.rst          |  12 +
 Documentation/gpu/todo.rst                     |   7 +
 drivers/gpu/drm/Kconfig                        |   6 +
 drivers/gpu/drm/Makefile                       |   1 +
 drivers/gpu/drm/drm_client.c                   |  12 +-
 drivers/gpu/drm/drm_gem.c                      | 109 ++++-
 drivers/gpu/drm/drm_gem_shmem_helper.c         | 551 +++++++++++++++++++++++++
 drivers/gpu/drm/drm_prime.c                    |  79 +++-
 drivers/gpu/drm/tinydrm/Kconfig                |   2 +-
 drivers/gpu/drm/tinydrm/core/tinydrm-core.c    |  92 ++---
 drivers/gpu/drm/tinydrm/core/tinydrm-helpers.c |   5 +
 drivers/gpu/drm/tinydrm/ili9225.c              |  14 +-
 drivers/gpu/drm/tinydrm/ili9341.c              |   6 +-
 drivers/gpu/drm/tinydrm/mi0283qt.c             |   6 +-
 drivers/gpu/drm/tinydrm/mipi-dbi.c             |  38 +-
 drivers/gpu/drm/tinydrm/repaper.c              |  24 +-
 drivers/gpu/drm/tinydrm/st7586.c               |  15 +-
 drivers/gpu/drm/tinydrm/st7735r.c              |   6 +-
 include/drm/drm_drv.h                          |   4 +
 include/drm/drm_gem.h                          | 131 ++++++
 include/drm/drm_gem_shmem_helper.h             | 153 +++++++
 include/drm/drm_prime.h                        |   1 +
 include/drm/tinydrm/tinydrm.h                  |  36 +-
 23 files changed, 1122 insertions(+), 188 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_gem_shmem_helper.c
 create mode 100644 include/drm/drm_gem_shmem_helper.h
    

Revisions