i915_gem.c 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951
  1. /*
  2. * Copyright © 2008-2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_vgpu.h"
  32. #include "i915_trace.h"
  33. #include "intel_drv.h"
  34. #include "intel_frontbuffer.h"
  35. #include "intel_mocs.h"
  36. #include <linux/dma-fence-array.h>
  37. #include <linux/reservation.h>
  38. #include <linux/shmem_fs.h>
  39. #include <linux/slab.h>
  40. #include <linux/stop_machine.h>
  41. #include <linux/swap.h>
  42. #include <linux/pci.h>
  43. #include <linux/dma-buf.h>
  44. static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
  45. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  46. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  47. static bool cpu_cache_is_coherent(struct drm_device *dev,
  48. enum i915_cache_level level)
  49. {
  50. return HAS_LLC(to_i915(dev)) || level != I915_CACHE_NONE;
  51. }
  52. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  53. {
  54. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  55. return false;
  56. if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  57. return true;
  58. return obj->pin_display;
  59. }
  60. static int
  61. insert_mappable_node(struct i915_ggtt *ggtt,
  62. struct drm_mm_node *node, u32 size)
  63. {
  64. memset(node, 0, sizeof(*node));
  65. return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
  66. size, 0, I915_COLOR_UNEVICTABLE,
  67. 0, ggtt->mappable_end,
  68. DRM_MM_INSERT_LOW);
  69. }
  70. static void
  71. remove_mappable_node(struct drm_mm_node *node)
  72. {
  73. drm_mm_remove_node(node);
  74. }
  75. /* some bookkeeping */
  76. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  77. u64 size)
  78. {
  79. spin_lock(&dev_priv->mm.object_stat_lock);
  80. dev_priv->mm.object_count++;
  81. dev_priv->mm.object_memory += size;
  82. spin_unlock(&dev_priv->mm.object_stat_lock);
  83. }
  84. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  85. u64 size)
  86. {
  87. spin_lock(&dev_priv->mm.object_stat_lock);
  88. dev_priv->mm.object_count--;
  89. dev_priv->mm.object_memory -= size;
  90. spin_unlock(&dev_priv->mm.object_stat_lock);
  91. }
  92. static int
  93. i915_gem_wait_for_error(struct i915_gpu_error *error)
  94. {
  95. int ret;
  96. might_sleep();
  97. if (!i915_reset_in_progress(error))
  98. return 0;
  99. /*
  100. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  101. * userspace. If it takes that long something really bad is going on and
  102. * we should simply try to bail out and fail as gracefully as possible.
  103. */
  104. ret = wait_event_interruptible_timeout(error->reset_queue,
  105. !i915_reset_in_progress(error),
  106. I915_RESET_TIMEOUT);
  107. if (ret == 0) {
  108. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  109. return -EIO;
  110. } else if (ret < 0) {
  111. return ret;
  112. } else {
  113. return 0;
  114. }
  115. }
  116. int i915_mutex_lock_interruptible(struct drm_device *dev)
  117. {
  118. struct drm_i915_private *dev_priv = to_i915(dev);
  119. int ret;
  120. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  121. if (ret)
  122. return ret;
  123. ret = mutex_lock_interruptible(&dev->struct_mutex);
  124. if (ret)
  125. return ret;
  126. return 0;
  127. }
  128. int
  129. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  130. struct drm_file *file)
  131. {
  132. struct drm_i915_private *dev_priv = to_i915(dev);
  133. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  134. struct drm_i915_gem_get_aperture *args = data;
  135. struct i915_vma *vma;
  136. size_t pinned;
  137. pinned = 0;
  138. mutex_lock(&dev->struct_mutex);
  139. list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
  140. if (i915_vma_is_pinned(vma))
  141. pinned += vma->node.size;
  142. list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
  143. if (i915_vma_is_pinned(vma))
  144. pinned += vma->node.size;
  145. mutex_unlock(&dev->struct_mutex);
  146. args->aper_size = ggtt->base.total;
  147. args->aper_available_size = args->aper_size - pinned;
  148. return 0;
  149. }
  150. static struct sg_table *
  151. i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  152. {
  153. struct address_space *mapping = obj->base.filp->f_mapping;
  154. drm_dma_handle_t *phys;
  155. struct sg_table *st;
  156. struct scatterlist *sg;
  157. char *vaddr;
  158. int i;
  159. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  160. return ERR_PTR(-EINVAL);
  161. /* Always aligning to the object size, allows a single allocation
  162. * to handle all possible callers, and given typical object sizes,
  163. * the alignment of the buddy allocation will naturally match.
  164. */
  165. phys = drm_pci_alloc(obj->base.dev,
  166. obj->base.size,
  167. roundup_pow_of_two(obj->base.size));
  168. if (!phys)
  169. return ERR_PTR(-ENOMEM);
  170. vaddr = phys->vaddr;
  171. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  172. struct page *page;
  173. char *src;
  174. page = shmem_read_mapping_page(mapping, i);
  175. if (IS_ERR(page)) {
  176. st = ERR_CAST(page);
  177. goto err_phys;
  178. }
  179. src = kmap_atomic(page);
  180. memcpy(vaddr, src, PAGE_SIZE);
  181. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  182. kunmap_atomic(src);
  183. put_page(page);
  184. vaddr += PAGE_SIZE;
  185. }
  186. i915_gem_chipset_flush(to_i915(obj->base.dev));
  187. st = kmalloc(sizeof(*st), GFP_KERNEL);
  188. if (!st) {
  189. st = ERR_PTR(-ENOMEM);
  190. goto err_phys;
  191. }
  192. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  193. kfree(st);
  194. st = ERR_PTR(-ENOMEM);
  195. goto err_phys;
  196. }
  197. sg = st->sgl;
  198. sg->offset = 0;
  199. sg->length = obj->base.size;
  200. sg_dma_address(sg) = phys->busaddr;
  201. sg_dma_len(sg) = obj->base.size;
  202. obj->phys_handle = phys;
  203. return st;
  204. err_phys:
  205. drm_pci_free(obj->base.dev, phys);
  206. return st;
  207. }
  208. static void
  209. __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
  210. struct sg_table *pages,
  211. bool needs_clflush)
  212. {
  213. GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
  214. if (obj->mm.madv == I915_MADV_DONTNEED)
  215. obj->mm.dirty = false;
  216. if (needs_clflush &&
  217. (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
  218. !cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  219. drm_clflush_sg(pages);
  220. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  221. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  222. }
  223. static void
  224. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
  225. struct sg_table *pages)
  226. {
  227. __i915_gem_object_release_shmem(obj, pages, false);
  228. if (obj->mm.dirty) {
  229. struct address_space *mapping = obj->base.filp->f_mapping;
  230. char *vaddr = obj->phys_handle->vaddr;
  231. int i;
  232. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  233. struct page *page;
  234. char *dst;
  235. page = shmem_read_mapping_page(mapping, i);
  236. if (IS_ERR(page))
  237. continue;
  238. dst = kmap_atomic(page);
  239. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  240. memcpy(dst, vaddr, PAGE_SIZE);
  241. kunmap_atomic(dst);
  242. set_page_dirty(page);
  243. if (obj->mm.madv == I915_MADV_WILLNEED)
  244. mark_page_accessed(page);
  245. put_page(page);
  246. vaddr += PAGE_SIZE;
  247. }
  248. obj->mm.dirty = false;
  249. }
  250. sg_free_table(pages);
  251. kfree(pages);
  252. drm_pci_free(obj->base.dev, obj->phys_handle);
  253. }
  254. static void
  255. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  256. {
  257. i915_gem_object_unpin_pages(obj);
  258. }
  259. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  260. .get_pages = i915_gem_object_get_pages_phys,
  261. .put_pages = i915_gem_object_put_pages_phys,
  262. .release = i915_gem_object_release_phys,
  263. };
  264. int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  265. {
  266. struct i915_vma *vma;
  267. LIST_HEAD(still_in_list);
  268. int ret;
  269. lockdep_assert_held(&obj->base.dev->struct_mutex);
  270. /* Closed vma are removed from the obj->vma_list - but they may
  271. * still have an active binding on the object. To remove those we
  272. * must wait for all rendering to complete to the object (as unbinding
  273. * must anyway), and retire the requests.
  274. */
  275. ret = i915_gem_object_wait(obj,
  276. I915_WAIT_INTERRUPTIBLE |
  277. I915_WAIT_LOCKED |
  278. I915_WAIT_ALL,
  279. MAX_SCHEDULE_TIMEOUT,
  280. NULL);
  281. if (ret)
  282. return ret;
  283. i915_gem_retire_requests(to_i915(obj->base.dev));
  284. while ((vma = list_first_entry_or_null(&obj->vma_list,
  285. struct i915_vma,
  286. obj_link))) {
  287. list_move_tail(&vma->obj_link, &still_in_list);
  288. ret = i915_vma_unbind(vma);
  289. if (ret)
  290. break;
  291. }
  292. list_splice(&still_in_list, &obj->vma_list);
  293. return ret;
  294. }
  295. static long
  296. i915_gem_object_wait_fence(struct dma_fence *fence,
  297. unsigned int flags,
  298. long timeout,
  299. struct intel_rps_client *rps)
  300. {
  301. struct drm_i915_gem_request *rq;
  302. BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
  303. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  304. return timeout;
  305. if (!dma_fence_is_i915(fence))
  306. return dma_fence_wait_timeout(fence,
  307. flags & I915_WAIT_INTERRUPTIBLE,
  308. timeout);
  309. rq = to_request(fence);
  310. if (i915_gem_request_completed(rq))
  311. goto out;
  312. /* This client is about to stall waiting for the GPU. In many cases
  313. * this is undesirable and limits the throughput of the system, as
  314. * many clients cannot continue processing user input/output whilst
  315. * blocked. RPS autotuning may take tens of milliseconds to respond
  316. * to the GPU load and thus incurs additional latency for the client.
  317. * We can circumvent that by promoting the GPU frequency to maximum
  318. * before we wait. This makes the GPU throttle up much more quickly
  319. * (good for benchmarks and user experience, e.g. window animations),
  320. * but at a cost of spending more power processing the workload
  321. * (bad for battery). Not all clients even want their results
  322. * immediately and for them we should just let the GPU select its own
  323. * frequency to maximise efficiency. To prevent a single client from
  324. * forcing the clocks too high for the whole system, we only allow
  325. * each client to waitboost once in a busy period.
  326. */
  327. if (rps) {
  328. if (INTEL_GEN(rq->i915) >= 6)
  329. gen6_rps_boost(rq->i915, rps, rq->emitted_jiffies);
  330. else
  331. rps = NULL;
  332. }
  333. timeout = i915_wait_request(rq, flags, timeout);
  334. out:
  335. if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
  336. i915_gem_request_retire_upto(rq);
  337. if (rps && rq->global_seqno == intel_engine_last_submit(rq->engine)) {
  338. /* The GPU is now idle and this client has stalled.
  339. * Since no other client has submitted a request in the
  340. * meantime, assume that this client is the only one
  341. * supplying work to the GPU but is unable to keep that
  342. * work supplied because it is waiting. Since the GPU is
  343. * then never kept fully busy, RPS autoclocking will
  344. * keep the clocks relatively low, causing further delays.
  345. * Compensate by giving the synchronous client credit for
  346. * a waitboost next time.
  347. */
  348. spin_lock(&rq->i915->rps.client_lock);
  349. list_del_init(&rps->link);
  350. spin_unlock(&rq->i915->rps.client_lock);
  351. }
  352. return timeout;
  353. }
  354. static long
  355. i915_gem_object_wait_reservation(struct reservation_object *resv,
  356. unsigned int flags,
  357. long timeout,
  358. struct intel_rps_client *rps)
  359. {
  360. struct dma_fence *excl;
  361. if (flags & I915_WAIT_ALL) {
  362. struct dma_fence **shared;
  363. unsigned int count, i;
  364. int ret;
  365. ret = reservation_object_get_fences_rcu(resv,
  366. &excl, &count, &shared);
  367. if (ret)
  368. return ret;
  369. for (i = 0; i < count; i++) {
  370. timeout = i915_gem_object_wait_fence(shared[i],
  371. flags, timeout,
  372. rps);
  373. if (timeout < 0)
  374. break;
  375. dma_fence_put(shared[i]);
  376. }
  377. for (; i < count; i++)
  378. dma_fence_put(shared[i]);
  379. kfree(shared);
  380. } else {
  381. excl = reservation_object_get_excl_rcu(resv);
  382. }
  383. if (excl && timeout >= 0)
  384. timeout = i915_gem_object_wait_fence(excl, flags, timeout, rps);
  385. dma_fence_put(excl);
  386. return timeout;
  387. }
  388. static void __fence_set_priority(struct dma_fence *fence, int prio)
  389. {
  390. struct drm_i915_gem_request *rq;
  391. struct intel_engine_cs *engine;
  392. if (!dma_fence_is_i915(fence))
  393. return;
  394. rq = to_request(fence);
  395. engine = rq->engine;
  396. if (!engine->schedule)
  397. return;
  398. engine->schedule(rq, prio);
  399. }
  400. static void fence_set_priority(struct dma_fence *fence, int prio)
  401. {
  402. /* Recurse once into a fence-array */
  403. if (dma_fence_is_array(fence)) {
  404. struct dma_fence_array *array = to_dma_fence_array(fence);
  405. int i;
  406. for (i = 0; i < array->num_fences; i++)
  407. __fence_set_priority(array->fences[i], prio);
  408. } else {
  409. __fence_set_priority(fence, prio);
  410. }
  411. }
  412. int
  413. i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
  414. unsigned int flags,
  415. int prio)
  416. {
  417. struct dma_fence *excl;
  418. if (flags & I915_WAIT_ALL) {
  419. struct dma_fence **shared;
  420. unsigned int count, i;
  421. int ret;
  422. ret = reservation_object_get_fences_rcu(obj->resv,
  423. &excl, &count, &shared);
  424. if (ret)
  425. return ret;
  426. for (i = 0; i < count; i++) {
  427. fence_set_priority(shared[i], prio);
  428. dma_fence_put(shared[i]);
  429. }
  430. kfree(shared);
  431. } else {
  432. excl = reservation_object_get_excl_rcu(obj->resv);
  433. }
  434. if (excl) {
  435. fence_set_priority(excl, prio);
  436. dma_fence_put(excl);
  437. }
  438. return 0;
  439. }
  440. /**
  441. * Waits for rendering to the object to be completed
  442. * @obj: i915 gem object
  443. * @flags: how to wait (under a lock, for all rendering or just for writes etc)
  444. * @timeout: how long to wait
  445. * @rps: client (user process) to charge for any waitboosting
  446. */
  447. int
  448. i915_gem_object_wait(struct drm_i915_gem_object *obj,
  449. unsigned int flags,
  450. long timeout,
  451. struct intel_rps_client *rps)
  452. {
  453. might_sleep();
  454. #if IS_ENABLED(CONFIG_LOCKDEP)
  455. GEM_BUG_ON(debug_locks &&
  456. !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
  457. !!(flags & I915_WAIT_LOCKED));
  458. #endif
  459. GEM_BUG_ON(timeout < 0);
  460. timeout = i915_gem_object_wait_reservation(obj->resv,
  461. flags, timeout,
  462. rps);
  463. return timeout < 0 ? timeout : 0;
  464. }
  465. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  466. {
  467. struct drm_i915_file_private *fpriv = file->driver_priv;
  468. return &fpriv->rps;
  469. }
  470. int
  471. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  472. int align)
  473. {
  474. int ret;
  475. if (align > obj->base.size)
  476. return -EINVAL;
  477. if (obj->ops == &i915_gem_phys_ops)
  478. return 0;
  479. if (obj->mm.madv != I915_MADV_WILLNEED)
  480. return -EFAULT;
  481. if (obj->base.filp == NULL)
  482. return -EINVAL;
  483. ret = i915_gem_object_unbind(obj);
  484. if (ret)
  485. return ret;
  486. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  487. if (obj->mm.pages)
  488. return -EBUSY;
  489. obj->ops = &i915_gem_phys_ops;
  490. return i915_gem_object_pin_pages(obj);
  491. }
  492. static int
  493. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  494. struct drm_i915_gem_pwrite *args,
  495. struct drm_file *file)
  496. {
  497. void *vaddr = obj->phys_handle->vaddr + args->offset;
  498. char __user *user_data = u64_to_user_ptr(args->data_ptr);
  499. /* We manually control the domain here and pretend that it
  500. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  501. */
  502. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  503. if (copy_from_user(vaddr, user_data, args->size))
  504. return -EFAULT;
  505. drm_clflush_virt_range(vaddr, args->size);
  506. i915_gem_chipset_flush(to_i915(obj->base.dev));
  507. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  508. return 0;
  509. }
  510. void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
  511. {
  512. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  513. }
  514. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  515. {
  516. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  517. kmem_cache_free(dev_priv->objects, obj);
  518. }
  519. static int
  520. i915_gem_create(struct drm_file *file,
  521. struct drm_i915_private *dev_priv,
  522. uint64_t size,
  523. uint32_t *handle_p)
  524. {
  525. struct drm_i915_gem_object *obj;
  526. int ret;
  527. u32 handle;
  528. size = roundup(size, PAGE_SIZE);
  529. if (size == 0)
  530. return -EINVAL;
  531. /* Allocate the new object */
  532. obj = i915_gem_object_create(dev_priv, size);
  533. if (IS_ERR(obj))
  534. return PTR_ERR(obj);
  535. ret = drm_gem_handle_create(file, &obj->base, &handle);
  536. /* drop reference from allocate - handle holds it now */
  537. i915_gem_object_put(obj);
  538. if (ret)
  539. return ret;
  540. *handle_p = handle;
  541. return 0;
  542. }
  543. int
  544. i915_gem_dumb_create(struct drm_file *file,
  545. struct drm_device *dev,
  546. struct drm_mode_create_dumb *args)
  547. {
  548. /* have to work out size/pitch and return them */
  549. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  550. args->size = args->pitch * args->height;
  551. return i915_gem_create(file, to_i915(dev),
  552. args->size, &args->handle);
  553. }
  554. /**
  555. * Creates a new mm object and returns a handle to it.
  556. * @dev: drm device pointer
  557. * @data: ioctl data blob
  558. * @file: drm file pointer
  559. */
  560. int
  561. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  562. struct drm_file *file)
  563. {
  564. struct drm_i915_private *dev_priv = to_i915(dev);
  565. struct drm_i915_gem_create *args = data;
  566. i915_gem_flush_free_objects(dev_priv);
  567. return i915_gem_create(file, dev_priv,
  568. args->size, &args->handle);
  569. }
  570. static inline int
  571. __copy_to_user_swizzled(char __user *cpu_vaddr,
  572. const char *gpu_vaddr, int gpu_offset,
  573. int length)
  574. {
  575. int ret, cpu_offset = 0;
  576. while (length > 0) {
  577. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  578. int this_length = min(cacheline_end - gpu_offset, length);
  579. int swizzled_gpu_offset = gpu_offset ^ 64;
  580. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  581. gpu_vaddr + swizzled_gpu_offset,
  582. this_length);
  583. if (ret)
  584. return ret + length;
  585. cpu_offset += this_length;
  586. gpu_offset += this_length;
  587. length -= this_length;
  588. }
  589. return 0;
  590. }
  591. static inline int
  592. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  593. const char __user *cpu_vaddr,
  594. int length)
  595. {
  596. int ret, cpu_offset = 0;
  597. while (length > 0) {
  598. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  599. int this_length = min(cacheline_end - gpu_offset, length);
  600. int swizzled_gpu_offset = gpu_offset ^ 64;
  601. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  602. cpu_vaddr + cpu_offset,
  603. this_length);
  604. if (ret)
  605. return ret + length;
  606. cpu_offset += this_length;
  607. gpu_offset += this_length;
  608. length -= this_length;
  609. }
  610. return 0;
  611. }
  612. /*
  613. * Pins the specified object's pages and synchronizes the object with
  614. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  615. * flush the object from the CPU cache.
  616. */
  617. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  618. unsigned int *needs_clflush)
  619. {
  620. int ret;
  621. lockdep_assert_held(&obj->base.dev->struct_mutex);
  622. *needs_clflush = 0;
  623. if (!i915_gem_object_has_struct_page(obj))
  624. return -ENODEV;
  625. ret = i915_gem_object_wait(obj,
  626. I915_WAIT_INTERRUPTIBLE |
  627. I915_WAIT_LOCKED,
  628. MAX_SCHEDULE_TIMEOUT,
  629. NULL);
  630. if (ret)
  631. return ret;
  632. ret = i915_gem_object_pin_pages(obj);
  633. if (ret)
  634. return ret;
  635. i915_gem_object_flush_gtt_write_domain(obj);
  636. /* If we're not in the cpu read domain, set ourself into the gtt
  637. * read domain and manually flush cachelines (if required). This
  638. * optimizes for the case when the gpu will dirty the data
  639. * anyway again before the next pread happens.
  640. */
  641. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  642. *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
  643. obj->cache_level);
  644. if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  645. ret = i915_gem_object_set_to_cpu_domain(obj, false);
  646. if (ret)
  647. goto err_unpin;
  648. *needs_clflush = 0;
  649. }
  650. /* return with the pages pinned */
  651. return 0;
  652. err_unpin:
  653. i915_gem_object_unpin_pages(obj);
  654. return ret;
  655. }
  656. int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
  657. unsigned int *needs_clflush)
  658. {
  659. int ret;
  660. lockdep_assert_held(&obj->base.dev->struct_mutex);
  661. *needs_clflush = 0;
  662. if (!i915_gem_object_has_struct_page(obj))
  663. return -ENODEV;
  664. ret = i915_gem_object_wait(obj,
  665. I915_WAIT_INTERRUPTIBLE |
  666. I915_WAIT_LOCKED |
  667. I915_WAIT_ALL,
  668. MAX_SCHEDULE_TIMEOUT,
  669. NULL);
  670. if (ret)
  671. return ret;
  672. ret = i915_gem_object_pin_pages(obj);
  673. if (ret)
  674. return ret;
  675. i915_gem_object_flush_gtt_write_domain(obj);
  676. /* If we're not in the cpu write domain, set ourself into the
  677. * gtt write domain and manually flush cachelines (as required).
  678. * This optimizes for the case when the gpu will use the data
  679. * right away and we therefore have to clflush anyway.
  680. */
  681. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  682. *needs_clflush |= cpu_write_needs_clflush(obj) << 1;
  683. /* Same trick applies to invalidate partially written cachelines read
  684. * before writing.
  685. */
  686. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  687. *needs_clflush |= !cpu_cache_is_coherent(obj->base.dev,
  688. obj->cache_level);
  689. if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  690. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  691. if (ret)
  692. goto err_unpin;
  693. *needs_clflush = 0;
  694. }
  695. if ((*needs_clflush & CLFLUSH_AFTER) == 0)
  696. obj->cache_dirty = true;
  697. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  698. obj->mm.dirty = true;
  699. /* return with the pages pinned */
  700. return 0;
  701. err_unpin:
  702. i915_gem_object_unpin_pages(obj);
  703. return ret;
  704. }
  705. static void
  706. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  707. bool swizzled)
  708. {
  709. if (unlikely(swizzled)) {
  710. unsigned long start = (unsigned long) addr;
  711. unsigned long end = (unsigned long) addr + length;
  712. /* For swizzling simply ensure that we always flush both
  713. * channels. Lame, but simple and it works. Swizzled
  714. * pwrite/pread is far from a hotpath - current userspace
  715. * doesn't use it at all. */
  716. start = round_down(start, 128);
  717. end = round_up(end, 128);
  718. drm_clflush_virt_range((void *)start, end - start);
  719. } else {
  720. drm_clflush_virt_range(addr, length);
  721. }
  722. }
  723. /* Only difference to the fast-path function is that this can handle bit17
  724. * and uses non-atomic copy and kmap functions. */
  725. static int
  726. shmem_pread_slow(struct page *page, int offset, int length,
  727. char __user *user_data,
  728. bool page_do_bit17_swizzling, bool needs_clflush)
  729. {
  730. char *vaddr;
  731. int ret;
  732. vaddr = kmap(page);
  733. if (needs_clflush)
  734. shmem_clflush_swizzled_range(vaddr + offset, length,
  735. page_do_bit17_swizzling);
  736. if (page_do_bit17_swizzling)
  737. ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
  738. else
  739. ret = __copy_to_user(user_data, vaddr + offset, length);
  740. kunmap(page);
  741. return ret ? - EFAULT : 0;
  742. }
  743. static int
  744. shmem_pread(struct page *page, int offset, int length, char __user *user_data,
  745. bool page_do_bit17_swizzling, bool needs_clflush)
  746. {
  747. int ret;
  748. ret = -ENODEV;
  749. if (!page_do_bit17_swizzling) {
  750. char *vaddr = kmap_atomic(page);
  751. if (needs_clflush)
  752. drm_clflush_virt_range(vaddr + offset, length);
  753. ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
  754. kunmap_atomic(vaddr);
  755. }
  756. if (ret == 0)
  757. return 0;
  758. return shmem_pread_slow(page, offset, length, user_data,
  759. page_do_bit17_swizzling, needs_clflush);
  760. }
  761. static int
  762. i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
  763. struct drm_i915_gem_pread *args)
  764. {
  765. char __user *user_data;
  766. u64 remain;
  767. unsigned int obj_do_bit17_swizzling;
  768. unsigned int needs_clflush;
  769. unsigned int idx, offset;
  770. int ret;
  771. obj_do_bit17_swizzling = 0;
  772. if (i915_gem_object_needs_bit17_swizzle(obj))
  773. obj_do_bit17_swizzling = BIT(17);
  774. ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
  775. if (ret)
  776. return ret;
  777. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  778. mutex_unlock(&obj->base.dev->struct_mutex);
  779. if (ret)
  780. return ret;
  781. remain = args->size;
  782. user_data = u64_to_user_ptr(args->data_ptr);
  783. offset = offset_in_page(args->offset);
  784. for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
  785. struct page *page = i915_gem_object_get_page(obj, idx);
  786. int length;
  787. length = remain;
  788. if (offset + length > PAGE_SIZE)
  789. length = PAGE_SIZE - offset;
  790. ret = shmem_pread(page, offset, length, user_data,
  791. page_to_phys(page) & obj_do_bit17_swizzling,
  792. needs_clflush);
  793. if (ret)
  794. break;
  795. remain -= length;
  796. user_data += length;
  797. offset = 0;
  798. }
  799. i915_gem_obj_finish_shmem_access(obj);
  800. return ret;
  801. }
  802. static inline bool
  803. gtt_user_read(struct io_mapping *mapping,
  804. loff_t base, int offset,
  805. char __user *user_data, int length)
  806. {
  807. void *vaddr;
  808. unsigned long unwritten;
  809. /* We can use the cpu mem copy function because this is X86. */
  810. vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
  811. unwritten = __copy_to_user_inatomic(user_data, vaddr + offset, length);
  812. io_mapping_unmap_atomic(vaddr);
  813. if (unwritten) {
  814. vaddr = (void __force *)
  815. io_mapping_map_wc(mapping, base, PAGE_SIZE);
  816. unwritten = copy_to_user(user_data, vaddr + offset, length);
  817. io_mapping_unmap(vaddr);
  818. }
  819. return unwritten;
  820. }
  821. static int
  822. i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
  823. const struct drm_i915_gem_pread *args)
  824. {
  825. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  826. struct i915_ggtt *ggtt = &i915->ggtt;
  827. struct drm_mm_node node;
  828. struct i915_vma *vma;
  829. void __user *user_data;
  830. u64 remain, offset;
  831. int ret;
  832. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  833. if (ret)
  834. return ret;
  835. intel_runtime_pm_get(i915);
  836. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  837. PIN_MAPPABLE | PIN_NONBLOCK);
  838. if (!IS_ERR(vma)) {
  839. node.start = i915_ggtt_offset(vma);
  840. node.allocated = false;
  841. ret = i915_vma_put_fence(vma);
  842. if (ret) {
  843. i915_vma_unpin(vma);
  844. vma = ERR_PTR(ret);
  845. }
  846. }
  847. if (IS_ERR(vma)) {
  848. ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
  849. if (ret)
  850. goto out_unlock;
  851. GEM_BUG_ON(!node.allocated);
  852. }
  853. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  854. if (ret)
  855. goto out_unpin;
  856. mutex_unlock(&i915->drm.struct_mutex);
  857. user_data = u64_to_user_ptr(args->data_ptr);
  858. remain = args->size;
  859. offset = args->offset;
  860. while (remain > 0) {
  861. /* Operation in this page
  862. *
  863. * page_base = page offset within aperture
  864. * page_offset = offset within page
  865. * page_length = bytes to copy for this page
  866. */
  867. u32 page_base = node.start;
  868. unsigned page_offset = offset_in_page(offset);
  869. unsigned page_length = PAGE_SIZE - page_offset;
  870. page_length = remain < page_length ? remain : page_length;
  871. if (node.allocated) {
  872. wmb();
  873. ggtt->base.insert_page(&ggtt->base,
  874. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  875. node.start, I915_CACHE_NONE, 0);
  876. wmb();
  877. } else {
  878. page_base += offset & PAGE_MASK;
  879. }
  880. if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
  881. user_data, page_length)) {
  882. ret = -EFAULT;
  883. break;
  884. }
  885. remain -= page_length;
  886. user_data += page_length;
  887. offset += page_length;
  888. }
  889. mutex_lock(&i915->drm.struct_mutex);
  890. out_unpin:
  891. if (node.allocated) {
  892. wmb();
  893. ggtt->base.clear_range(&ggtt->base,
  894. node.start, node.size);
  895. remove_mappable_node(&node);
  896. } else {
  897. i915_vma_unpin(vma);
  898. }
  899. out_unlock:
  900. intel_runtime_pm_put(i915);
  901. mutex_unlock(&i915->drm.struct_mutex);
  902. return ret;
  903. }
  904. /**
  905. * Reads data from the object referenced by handle.
  906. * @dev: drm device pointer
  907. * @data: ioctl data blob
  908. * @file: drm file pointer
  909. *
  910. * On error, the contents of *data are undefined.
  911. */
  912. int
  913. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  914. struct drm_file *file)
  915. {
  916. struct drm_i915_gem_pread *args = data;
  917. struct drm_i915_gem_object *obj;
  918. int ret;
  919. if (args->size == 0)
  920. return 0;
  921. if (!access_ok(VERIFY_WRITE,
  922. u64_to_user_ptr(args->data_ptr),
  923. args->size))
  924. return -EFAULT;
  925. obj = i915_gem_object_lookup(file, args->handle);
  926. if (!obj)
  927. return -ENOENT;
  928. /* Bounds check source. */
  929. if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
  930. ret = -EINVAL;
  931. goto out;
  932. }
  933. trace_i915_gem_object_pread(obj, args->offset, args->size);
  934. ret = i915_gem_object_wait(obj,
  935. I915_WAIT_INTERRUPTIBLE,
  936. MAX_SCHEDULE_TIMEOUT,
  937. to_rps_client(file));
  938. if (ret)
  939. goto out;
  940. ret = i915_gem_object_pin_pages(obj);
  941. if (ret)
  942. goto out;
  943. ret = i915_gem_shmem_pread(obj, args);
  944. if (ret == -EFAULT || ret == -ENODEV)
  945. ret = i915_gem_gtt_pread(obj, args);
  946. i915_gem_object_unpin_pages(obj);
  947. out:
  948. i915_gem_object_put(obj);
  949. return ret;
  950. }
  951. /* This is the fast write path which cannot handle
  952. * page faults in the source data
  953. */
  954. static inline bool
  955. ggtt_write(struct io_mapping *mapping,
  956. loff_t base, int offset,
  957. char __user *user_data, int length)
  958. {
  959. void *vaddr;
  960. unsigned long unwritten;
  961. /* We can use the cpu mem copy function because this is X86. */
  962. vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
  963. unwritten = __copy_from_user_inatomic_nocache(vaddr + offset,
  964. user_data, length);
  965. io_mapping_unmap_atomic(vaddr);
  966. if (unwritten) {
  967. vaddr = (void __force *)
  968. io_mapping_map_wc(mapping, base, PAGE_SIZE);
  969. unwritten = copy_from_user(vaddr + offset, user_data, length);
  970. io_mapping_unmap(vaddr);
  971. }
  972. return unwritten;
  973. }
  974. /**
  975. * This is the fast pwrite path, where we copy the data directly from the
  976. * user into the GTT, uncached.
  977. * @obj: i915 GEM object
  978. * @args: pwrite arguments structure
  979. */
  980. static int
  981. i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
  982. const struct drm_i915_gem_pwrite *args)
  983. {
  984. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  985. struct i915_ggtt *ggtt = &i915->ggtt;
  986. struct drm_mm_node node;
  987. struct i915_vma *vma;
  988. u64 remain, offset;
  989. void __user *user_data;
  990. int ret;
  991. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  992. if (ret)
  993. return ret;
  994. intel_runtime_pm_get(i915);
  995. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  996. PIN_MAPPABLE | PIN_NONBLOCK);
  997. if (!IS_ERR(vma)) {
  998. node.start = i915_ggtt_offset(vma);
  999. node.allocated = false;
  1000. ret = i915_vma_put_fence(vma);
  1001. if (ret) {
  1002. i915_vma_unpin(vma);
  1003. vma = ERR_PTR(ret);
  1004. }
  1005. }
  1006. if (IS_ERR(vma)) {
  1007. ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
  1008. if (ret)
  1009. goto out_unlock;
  1010. GEM_BUG_ON(!node.allocated);
  1011. }
  1012. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  1013. if (ret)
  1014. goto out_unpin;
  1015. mutex_unlock(&i915->drm.struct_mutex);
  1016. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  1017. user_data = u64_to_user_ptr(args->data_ptr);
  1018. offset = args->offset;
  1019. remain = args->size;
  1020. while (remain) {
  1021. /* Operation in this page
  1022. *
  1023. * page_base = page offset within aperture
  1024. * page_offset = offset within page
  1025. * page_length = bytes to copy for this page
  1026. */
  1027. u32 page_base = node.start;
  1028. unsigned int page_offset = offset_in_page(offset);
  1029. unsigned int page_length = PAGE_SIZE - page_offset;
  1030. page_length = remain < page_length ? remain : page_length;
  1031. if (node.allocated) {
  1032. wmb(); /* flush the write before we modify the GGTT */
  1033. ggtt->base.insert_page(&ggtt->base,
  1034. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  1035. node.start, I915_CACHE_NONE, 0);
  1036. wmb(); /* flush modifications to the GGTT (insert_page) */
  1037. } else {
  1038. page_base += offset & PAGE_MASK;
  1039. }
  1040. /* If we get a fault while copying data, then (presumably) our
  1041. * source page isn't available. Return the error and we'll
  1042. * retry in the slow path.
  1043. * If the object is non-shmem backed, we retry again with the
  1044. * path that handles page fault.
  1045. */
  1046. if (ggtt_write(&ggtt->mappable, page_base, page_offset,
  1047. user_data, page_length)) {
  1048. ret = -EFAULT;
  1049. break;
  1050. }
  1051. remain -= page_length;
  1052. user_data += page_length;
  1053. offset += page_length;
  1054. }
  1055. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  1056. mutex_lock(&i915->drm.struct_mutex);
  1057. out_unpin:
  1058. if (node.allocated) {
  1059. wmb();
  1060. ggtt->base.clear_range(&ggtt->base,
  1061. node.start, node.size);
  1062. remove_mappable_node(&node);
  1063. } else {
  1064. i915_vma_unpin(vma);
  1065. }
  1066. out_unlock:
  1067. intel_runtime_pm_put(i915);
  1068. mutex_unlock(&i915->drm.struct_mutex);
  1069. return ret;
  1070. }
  1071. static int
  1072. shmem_pwrite_slow(struct page *page, int offset, int length,
  1073. char __user *user_data,
  1074. bool page_do_bit17_swizzling,
  1075. bool needs_clflush_before,
  1076. bool needs_clflush_after)
  1077. {
  1078. char *vaddr;
  1079. int ret;
  1080. vaddr = kmap(page);
  1081. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  1082. shmem_clflush_swizzled_range(vaddr + offset, length,
  1083. page_do_bit17_swizzling);
  1084. if (page_do_bit17_swizzling)
  1085. ret = __copy_from_user_swizzled(vaddr, offset, user_data,
  1086. length);
  1087. else
  1088. ret = __copy_from_user(vaddr + offset, user_data, length);
  1089. if (needs_clflush_after)
  1090. shmem_clflush_swizzled_range(vaddr + offset, length,
  1091. page_do_bit17_swizzling);
  1092. kunmap(page);
  1093. return ret ? -EFAULT : 0;
  1094. }
  1095. /* Per-page copy function for the shmem pwrite fastpath.
  1096. * Flushes invalid cachelines before writing to the target if
  1097. * needs_clflush_before is set and flushes out any written cachelines after
  1098. * writing if needs_clflush is set.
  1099. */
  1100. static int
  1101. shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
  1102. bool page_do_bit17_swizzling,
  1103. bool needs_clflush_before,
  1104. bool needs_clflush_after)
  1105. {
  1106. int ret;
  1107. ret = -ENODEV;
  1108. if (!page_do_bit17_swizzling) {
  1109. char *vaddr = kmap_atomic(page);
  1110. if (needs_clflush_before)
  1111. drm_clflush_virt_range(vaddr + offset, len);
  1112. ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
  1113. if (needs_clflush_after)
  1114. drm_clflush_virt_range(vaddr + offset, len);
  1115. kunmap_atomic(vaddr);
  1116. }
  1117. if (ret == 0)
  1118. return ret;
  1119. return shmem_pwrite_slow(page, offset, len, user_data,
  1120. page_do_bit17_swizzling,
  1121. needs_clflush_before,
  1122. needs_clflush_after);
  1123. }
  1124. static int
  1125. i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
  1126. const struct drm_i915_gem_pwrite *args)
  1127. {
  1128. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1129. void __user *user_data;
  1130. u64 remain;
  1131. unsigned int obj_do_bit17_swizzling;
  1132. unsigned int partial_cacheline_write;
  1133. unsigned int needs_clflush;
  1134. unsigned int offset, idx;
  1135. int ret;
  1136. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  1137. if (ret)
  1138. return ret;
  1139. ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
  1140. mutex_unlock(&i915->drm.struct_mutex);
  1141. if (ret)
  1142. return ret;
  1143. obj_do_bit17_swizzling = 0;
  1144. if (i915_gem_object_needs_bit17_swizzle(obj))
  1145. obj_do_bit17_swizzling = BIT(17);
  1146. /* If we don't overwrite a cacheline completely we need to be
  1147. * careful to have up-to-date data by first clflushing. Don't
  1148. * overcomplicate things and flush the entire patch.
  1149. */
  1150. partial_cacheline_write = 0;
  1151. if (needs_clflush & CLFLUSH_BEFORE)
  1152. partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
  1153. user_data = u64_to_user_ptr(args->data_ptr);
  1154. remain = args->size;
  1155. offset = offset_in_page(args->offset);
  1156. for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
  1157. struct page *page = i915_gem_object_get_page(obj, idx);
  1158. int length;
  1159. length = remain;
  1160. if (offset + length > PAGE_SIZE)
  1161. length = PAGE_SIZE - offset;
  1162. ret = shmem_pwrite(page, offset, length, user_data,
  1163. page_to_phys(page) & obj_do_bit17_swizzling,
  1164. (offset | length) & partial_cacheline_write,
  1165. needs_clflush & CLFLUSH_AFTER);
  1166. if (ret)
  1167. break;
  1168. remain -= length;
  1169. user_data += length;
  1170. offset = 0;
  1171. }
  1172. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  1173. i915_gem_obj_finish_shmem_access(obj);
  1174. return ret;
  1175. }
  1176. /**
  1177. * Writes data to the object referenced by handle.
  1178. * @dev: drm device
  1179. * @data: ioctl data blob
  1180. * @file: drm file
  1181. *
  1182. * On error, the contents of the buffer that were to be modified are undefined.
  1183. */
  1184. int
  1185. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  1186. struct drm_file *file)
  1187. {
  1188. struct drm_i915_gem_pwrite *args = data;
  1189. struct drm_i915_gem_object *obj;
  1190. int ret;
  1191. if (args->size == 0)
  1192. return 0;
  1193. if (!access_ok(VERIFY_READ,
  1194. u64_to_user_ptr(args->data_ptr),
  1195. args->size))
  1196. return -EFAULT;
  1197. obj = i915_gem_object_lookup(file, args->handle);
  1198. if (!obj)
  1199. return -ENOENT;
  1200. /* Bounds check destination. */
  1201. if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
  1202. ret = -EINVAL;
  1203. goto err;
  1204. }
  1205. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  1206. ret = i915_gem_object_wait(obj,
  1207. I915_WAIT_INTERRUPTIBLE |
  1208. I915_WAIT_ALL,
  1209. MAX_SCHEDULE_TIMEOUT,
  1210. to_rps_client(file));
  1211. if (ret)
  1212. goto err;
  1213. ret = i915_gem_object_pin_pages(obj);
  1214. if (ret)
  1215. goto err;
  1216. ret = -EFAULT;
  1217. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  1218. * it would end up going through the fenced access, and we'll get
  1219. * different detiling behavior between reading and writing.
  1220. * pread/pwrite currently are reading and writing from the CPU
  1221. * perspective, requiring manual detiling by the client.
  1222. */
  1223. if (!i915_gem_object_has_struct_page(obj) ||
  1224. cpu_write_needs_clflush(obj))
  1225. /* Note that the gtt paths might fail with non-page-backed user
  1226. * pointers (e.g. gtt mappings when moving data between
  1227. * textures). Fallback to the shmem path in that case.
  1228. */
  1229. ret = i915_gem_gtt_pwrite_fast(obj, args);
  1230. if (ret == -EFAULT || ret == -ENOSPC) {
  1231. if (obj->phys_handle)
  1232. ret = i915_gem_phys_pwrite(obj, args, file);
  1233. else
  1234. ret = i915_gem_shmem_pwrite(obj, args);
  1235. }
  1236. i915_gem_object_unpin_pages(obj);
  1237. err:
  1238. i915_gem_object_put(obj);
  1239. return ret;
  1240. }
  1241. static inline enum fb_op_origin
  1242. write_origin(struct drm_i915_gem_object *obj, unsigned domain)
  1243. {
  1244. return (domain == I915_GEM_DOMAIN_GTT ?
  1245. obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
  1246. }
  1247. static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
  1248. {
  1249. struct drm_i915_private *i915;
  1250. struct list_head *list;
  1251. struct i915_vma *vma;
  1252. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  1253. if (!i915_vma_is_ggtt(vma))
  1254. break;
  1255. if (i915_vma_is_active(vma))
  1256. continue;
  1257. if (!drm_mm_node_allocated(&vma->node))
  1258. continue;
  1259. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  1260. }
  1261. i915 = to_i915(obj->base.dev);
  1262. list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
  1263. list_move_tail(&obj->global_link, list);
  1264. }
  1265. /**
  1266. * Called when user space prepares to use an object with the CPU, either
  1267. * through the mmap ioctl's mapping or a GTT mapping.
  1268. * @dev: drm device
  1269. * @data: ioctl data blob
  1270. * @file: drm file
  1271. */
  1272. int
  1273. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1274. struct drm_file *file)
  1275. {
  1276. struct drm_i915_gem_set_domain *args = data;
  1277. struct drm_i915_gem_object *obj;
  1278. uint32_t read_domains = args->read_domains;
  1279. uint32_t write_domain = args->write_domain;
  1280. int err;
  1281. /* Only handle setting domains to types used by the CPU. */
  1282. if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
  1283. return -EINVAL;
  1284. /* Having something in the write domain implies it's in the read
  1285. * domain, and only that read domain. Enforce that in the request.
  1286. */
  1287. if (write_domain != 0 && read_domains != write_domain)
  1288. return -EINVAL;
  1289. obj = i915_gem_object_lookup(file, args->handle);
  1290. if (!obj)
  1291. return -ENOENT;
  1292. /* Try to flush the object off the GPU without holding the lock.
  1293. * We will repeat the flush holding the lock in the normal manner
  1294. * to catch cases where we are gazumped.
  1295. */
  1296. err = i915_gem_object_wait(obj,
  1297. I915_WAIT_INTERRUPTIBLE |
  1298. (write_domain ? I915_WAIT_ALL : 0),
  1299. MAX_SCHEDULE_TIMEOUT,
  1300. to_rps_client(file));
  1301. if (err)
  1302. goto out;
  1303. /* Flush and acquire obj->pages so that we are coherent through
  1304. * direct access in memory with previous cached writes through
  1305. * shmemfs and that our cache domain tracking remains valid.
  1306. * For example, if the obj->filp was moved to swap without us
  1307. * being notified and releasing the pages, we would mistakenly
  1308. * continue to assume that the obj remained out of the CPU cached
  1309. * domain.
  1310. */
  1311. err = i915_gem_object_pin_pages(obj);
  1312. if (err)
  1313. goto out;
  1314. err = i915_mutex_lock_interruptible(dev);
  1315. if (err)
  1316. goto out_unpin;
  1317. if (read_domains & I915_GEM_DOMAIN_GTT)
  1318. err = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1319. else
  1320. err = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1321. /* And bump the LRU for this access */
  1322. i915_gem_object_bump_inactive_ggtt(obj);
  1323. mutex_unlock(&dev->struct_mutex);
  1324. if (write_domain != 0)
  1325. intel_fb_obj_invalidate(obj, write_origin(obj, write_domain));
  1326. out_unpin:
  1327. i915_gem_object_unpin_pages(obj);
  1328. out:
  1329. i915_gem_object_put(obj);
  1330. return err;
  1331. }
  1332. /**
  1333. * Called when user space has done writes to this buffer
  1334. * @dev: drm device
  1335. * @data: ioctl data blob
  1336. * @file: drm file
  1337. */
  1338. int
  1339. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1340. struct drm_file *file)
  1341. {
  1342. struct drm_i915_gem_sw_finish *args = data;
  1343. struct drm_i915_gem_object *obj;
  1344. int err = 0;
  1345. obj = i915_gem_object_lookup(file, args->handle);
  1346. if (!obj)
  1347. return -ENOENT;
  1348. /* Pinned buffers may be scanout, so flush the cache */
  1349. if (READ_ONCE(obj->pin_display)) {
  1350. err = i915_mutex_lock_interruptible(dev);
  1351. if (!err) {
  1352. i915_gem_object_flush_cpu_write_domain(obj);
  1353. mutex_unlock(&dev->struct_mutex);
  1354. }
  1355. }
  1356. i915_gem_object_put(obj);
  1357. return err;
  1358. }
  1359. /**
  1360. * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
  1361. * it is mapped to.
  1362. * @dev: drm device
  1363. * @data: ioctl data blob
  1364. * @file: drm file
  1365. *
  1366. * While the mapping holds a reference on the contents of the object, it doesn't
  1367. * imply a ref on the object itself.
  1368. *
  1369. * IMPORTANT:
  1370. *
  1371. * DRM driver writers who look a this function as an example for how to do GEM
  1372. * mmap support, please don't implement mmap support like here. The modern way
  1373. * to implement DRM mmap support is with an mmap offset ioctl (like
  1374. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1375. * That way debug tooling like valgrind will understand what's going on, hiding
  1376. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1377. * does cpu mmaps this way because we didn't know better.
  1378. */
  1379. int
  1380. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1381. struct drm_file *file)
  1382. {
  1383. struct drm_i915_gem_mmap *args = data;
  1384. struct drm_i915_gem_object *obj;
  1385. unsigned long addr;
  1386. if (args->flags & ~(I915_MMAP_WC))
  1387. return -EINVAL;
  1388. if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
  1389. return -ENODEV;
  1390. obj = i915_gem_object_lookup(file, args->handle);
  1391. if (!obj)
  1392. return -ENOENT;
  1393. /* prime objects have no backing filp to GEM mmap
  1394. * pages from.
  1395. */
  1396. if (!obj->base.filp) {
  1397. i915_gem_object_put(obj);
  1398. return -EINVAL;
  1399. }
  1400. addr = vm_mmap(obj->base.filp, 0, args->size,
  1401. PROT_READ | PROT_WRITE, MAP_SHARED,
  1402. args->offset);
  1403. if (args->flags & I915_MMAP_WC) {
  1404. struct mm_struct *mm = current->mm;
  1405. struct vm_area_struct *vma;
  1406. if (down_write_killable(&mm->mmap_sem)) {
  1407. i915_gem_object_put(obj);
  1408. return -EINTR;
  1409. }
  1410. vma = find_vma(mm, addr);
  1411. if (vma)
  1412. vma->vm_page_prot =
  1413. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1414. else
  1415. addr = -ENOMEM;
  1416. up_write(&mm->mmap_sem);
  1417. /* This may race, but that's ok, it only gets set */
  1418. WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
  1419. }
  1420. i915_gem_object_put(obj);
  1421. if (IS_ERR((void *)addr))
  1422. return addr;
  1423. args->addr_ptr = (uint64_t) addr;
  1424. return 0;
  1425. }
  1426. static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
  1427. {
  1428. return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
  1429. }
  1430. /**
  1431. * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
  1432. *
  1433. * A history of the GTT mmap interface:
  1434. *
  1435. * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
  1436. * aligned and suitable for fencing, and still fit into the available
  1437. * mappable space left by the pinned display objects. A classic problem
  1438. * we called the page-fault-of-doom where we would ping-pong between
  1439. * two objects that could not fit inside the GTT and so the memcpy
  1440. * would page one object in at the expense of the other between every
  1441. * single byte.
  1442. *
  1443. * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
  1444. * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
  1445. * object is too large for the available space (or simply too large
  1446. * for the mappable aperture!), a view is created instead and faulted
  1447. * into userspace. (This view is aligned and sized appropriately for
  1448. * fenced access.)
  1449. *
  1450. * Restrictions:
  1451. *
  1452. * * snoopable objects cannot be accessed via the GTT. It can cause machine
  1453. * hangs on some architectures, corruption on others. An attempt to service
  1454. * a GTT page fault from a snoopable object will generate a SIGBUS.
  1455. *
  1456. * * the object must be able to fit into RAM (physical memory, though no
  1457. * limited to the mappable aperture).
  1458. *
  1459. *
  1460. * Caveats:
  1461. *
  1462. * * a new GTT page fault will synchronize rendering from the GPU and flush
  1463. * all data to system memory. Subsequent access will not be synchronized.
  1464. *
  1465. * * all mappings are revoked on runtime device suspend.
  1466. *
  1467. * * there are only 8, 16 or 32 fence registers to share between all users
  1468. * (older machines require fence register for display and blitter access
  1469. * as well). Contention of the fence registers will cause the previous users
  1470. * to be unmapped and any new access will generate new page faults.
  1471. *
  1472. * * running out of memory while servicing a fault may generate a SIGBUS,
  1473. * rather than the expected SIGSEGV.
  1474. */
  1475. int i915_gem_mmap_gtt_version(void)
  1476. {
  1477. return 1;
  1478. }
  1479. static inline struct i915_ggtt_view
  1480. compute_partial_view(struct drm_i915_gem_object *obj,
  1481. pgoff_t page_offset,
  1482. unsigned int chunk)
  1483. {
  1484. struct i915_ggtt_view view;
  1485. if (i915_gem_object_is_tiled(obj))
  1486. chunk = roundup(chunk, tile_row_pages(obj));
  1487. view.type = I915_GGTT_VIEW_PARTIAL;
  1488. view.partial.offset = rounddown(page_offset, chunk);
  1489. view.partial.size =
  1490. min_t(unsigned int, chunk,
  1491. (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
  1492. /* If the partial covers the entire object, just create a normal VMA. */
  1493. if (chunk >= obj->base.size >> PAGE_SHIFT)
  1494. view.type = I915_GGTT_VIEW_NORMAL;
  1495. return view;
  1496. }
  1497. /**
  1498. * i915_gem_fault - fault a page into the GTT
  1499. * @vmf: fault info
  1500. *
  1501. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1502. * from userspace. The fault handler takes care of binding the object to
  1503. * the GTT (if needed), allocating and programming a fence register (again,
  1504. * only if needed based on whether the old reg is still valid or the object
  1505. * is tiled) and inserting a new PTE into the faulting process.
  1506. *
  1507. * Note that the faulting process may involve evicting existing objects
  1508. * from the GTT and/or fence registers to make room. So performance may
  1509. * suffer if the GTT working set is large or there are few fence registers
  1510. * left.
  1511. *
  1512. * The current feature set supported by i915_gem_fault() and thus GTT mmaps
  1513. * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
  1514. */
  1515. int i915_gem_fault(struct vm_fault *vmf)
  1516. {
  1517. #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
  1518. struct vm_area_struct *area = vmf->vma;
  1519. struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
  1520. struct drm_device *dev = obj->base.dev;
  1521. struct drm_i915_private *dev_priv = to_i915(dev);
  1522. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1523. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1524. struct i915_vma *vma;
  1525. pgoff_t page_offset;
  1526. unsigned int flags;
  1527. int ret;
  1528. /* We don't use vmf->pgoff since that has the fake offset */
  1529. page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
  1530. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1531. /* Try to flush the object off the GPU first without holding the lock.
  1532. * Upon acquiring the lock, we will perform our sanity checks and then
  1533. * repeat the flush holding the lock in the normal manner to catch cases
  1534. * where we are gazumped.
  1535. */
  1536. ret = i915_gem_object_wait(obj,
  1537. I915_WAIT_INTERRUPTIBLE,
  1538. MAX_SCHEDULE_TIMEOUT,
  1539. NULL);
  1540. if (ret)
  1541. goto err;
  1542. ret = i915_gem_object_pin_pages(obj);
  1543. if (ret)
  1544. goto err;
  1545. intel_runtime_pm_get(dev_priv);
  1546. ret = i915_mutex_lock_interruptible(dev);
  1547. if (ret)
  1548. goto err_rpm;
  1549. /* Access to snoopable pages through the GTT is incoherent. */
  1550. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
  1551. ret = -EFAULT;
  1552. goto err_unlock;
  1553. }
  1554. /* If the object is smaller than a couple of partial vma, it is
  1555. * not worth only creating a single partial vma - we may as well
  1556. * clear enough space for the full object.
  1557. */
  1558. flags = PIN_MAPPABLE;
  1559. if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
  1560. flags |= PIN_NONBLOCK | PIN_NONFAULT;
  1561. /* Now pin it into the GTT as needed */
  1562. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
  1563. if (IS_ERR(vma)) {
  1564. /* Use a partial view if it is bigger than available space */
  1565. struct i915_ggtt_view view =
  1566. compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
  1567. /* Userspace is now writing through an untracked VMA, abandon
  1568. * all hope that the hardware is able to track future writes.
  1569. */
  1570. obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
  1571. vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
  1572. }
  1573. if (IS_ERR(vma)) {
  1574. ret = PTR_ERR(vma);
  1575. goto err_unlock;
  1576. }
  1577. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1578. if (ret)
  1579. goto err_unpin;
  1580. ret = i915_vma_get_fence(vma);
  1581. if (ret)
  1582. goto err_unpin;
  1583. /* Mark as being mmapped into userspace for later revocation */
  1584. assert_rpm_wakelock_held(dev_priv);
  1585. if (list_empty(&obj->userfault_link))
  1586. list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
  1587. /* Finally, remap it using the new GTT offset */
  1588. ret = remap_io_mapping(area,
  1589. area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
  1590. (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
  1591. min_t(u64, vma->size, area->vm_end - area->vm_start),
  1592. &ggtt->mappable);
  1593. err_unpin:
  1594. __i915_vma_unpin(vma);
  1595. err_unlock:
  1596. mutex_unlock(&dev->struct_mutex);
  1597. err_rpm:
  1598. intel_runtime_pm_put(dev_priv);
  1599. i915_gem_object_unpin_pages(obj);
  1600. err:
  1601. switch (ret) {
  1602. case -EIO:
  1603. /*
  1604. * We eat errors when the gpu is terminally wedged to avoid
  1605. * userspace unduly crashing (gl has no provisions for mmaps to
  1606. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1607. * and so needs to be reported.
  1608. */
  1609. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1610. ret = VM_FAULT_SIGBUS;
  1611. break;
  1612. }
  1613. case -EAGAIN:
  1614. /*
  1615. * EAGAIN means the gpu is hung and we'll wait for the error
  1616. * handler to reset everything when re-faulting in
  1617. * i915_mutex_lock_interruptible.
  1618. */
  1619. case 0:
  1620. case -ERESTARTSYS:
  1621. case -EINTR:
  1622. case -EBUSY:
  1623. /*
  1624. * EBUSY is ok: this just means that another thread
  1625. * already did the job.
  1626. */
  1627. ret = VM_FAULT_NOPAGE;
  1628. break;
  1629. case -ENOMEM:
  1630. ret = VM_FAULT_OOM;
  1631. break;
  1632. case -ENOSPC:
  1633. case -EFAULT:
  1634. ret = VM_FAULT_SIGBUS;
  1635. break;
  1636. default:
  1637. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1638. ret = VM_FAULT_SIGBUS;
  1639. break;
  1640. }
  1641. return ret;
  1642. }
  1643. /**
  1644. * i915_gem_release_mmap - remove physical page mappings
  1645. * @obj: obj in question
  1646. *
  1647. * Preserve the reservation of the mmapping with the DRM core code, but
  1648. * relinquish ownership of the pages back to the system.
  1649. *
  1650. * It is vital that we remove the page mapping if we have mapped a tiled
  1651. * object through the GTT and then lose the fence register due to
  1652. * resource pressure. Similarly if the object has been moved out of the
  1653. * aperture, than pages mapped into userspace must be revoked. Removing the
  1654. * mapping will then trigger a page fault on the next user access, allowing
  1655. * fixup by i915_gem_fault().
  1656. */
  1657. void
  1658. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1659. {
  1660. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1661. /* Serialisation between user GTT access and our code depends upon
  1662. * revoking the CPU's PTE whilst the mutex is held. The next user
  1663. * pagefault then has to wait until we release the mutex.
  1664. *
  1665. * Note that RPM complicates somewhat by adding an additional
  1666. * requirement that operations to the GGTT be made holding the RPM
  1667. * wakeref.
  1668. */
  1669. lockdep_assert_held(&i915->drm.struct_mutex);
  1670. intel_runtime_pm_get(i915);
  1671. if (list_empty(&obj->userfault_link))
  1672. goto out;
  1673. list_del_init(&obj->userfault_link);
  1674. drm_vma_node_unmap(&obj->base.vma_node,
  1675. obj->base.dev->anon_inode->i_mapping);
  1676. /* Ensure that the CPU's PTE are revoked and there are not outstanding
  1677. * memory transactions from userspace before we return. The TLB
  1678. * flushing implied above by changing the PTE above *should* be
  1679. * sufficient, an extra barrier here just provides us with a bit
  1680. * of paranoid documentation about our requirement to serialise
  1681. * memory writes before touching registers / GSM.
  1682. */
  1683. wmb();
  1684. out:
  1685. intel_runtime_pm_put(i915);
  1686. }
  1687. void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
  1688. {
  1689. struct drm_i915_gem_object *obj, *on;
  1690. int i;
  1691. /*
  1692. * Only called during RPM suspend. All users of the userfault_list
  1693. * must be holding an RPM wakeref to ensure that this can not
  1694. * run concurrently with themselves (and use the struct_mutex for
  1695. * protection between themselves).
  1696. */
  1697. list_for_each_entry_safe(obj, on,
  1698. &dev_priv->mm.userfault_list, userfault_link) {
  1699. list_del_init(&obj->userfault_link);
  1700. drm_vma_node_unmap(&obj->base.vma_node,
  1701. obj->base.dev->anon_inode->i_mapping);
  1702. }
  1703. /* The fence will be lost when the device powers down. If any were
  1704. * in use by hardware (i.e. they are pinned), we should not be powering
  1705. * down! All other fences will be reacquired by the user upon waking.
  1706. */
  1707. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1708. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1709. /* Ideally we want to assert that the fence register is not
  1710. * live at this point (i.e. that no piece of code will be
  1711. * trying to write through fence + GTT, as that both violates
  1712. * our tracking of activity and associated locking/barriers,
  1713. * but also is illegal given that the hw is powered down).
  1714. *
  1715. * Previously we used reg->pin_count as a "liveness" indicator.
  1716. * That is not sufficient, and we need a more fine-grained
  1717. * tool if we want to have a sanity check here.
  1718. */
  1719. if (!reg->vma)
  1720. continue;
  1721. GEM_BUG_ON(!list_empty(&reg->vma->obj->userfault_link));
  1722. reg->dirty = true;
  1723. }
  1724. }
  1725. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1726. {
  1727. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1728. int err;
  1729. err = drm_gem_create_mmap_offset(&obj->base);
  1730. if (likely(!err))
  1731. return 0;
  1732. /* Attempt to reap some mmap space from dead objects */
  1733. do {
  1734. err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
  1735. if (err)
  1736. break;
  1737. i915_gem_drain_freed_objects(dev_priv);
  1738. err = drm_gem_create_mmap_offset(&obj->base);
  1739. if (!err)
  1740. break;
  1741. } while (flush_delayed_work(&dev_priv->gt.retire_work));
  1742. return err;
  1743. }
  1744. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1745. {
  1746. drm_gem_free_mmap_offset(&obj->base);
  1747. }
  1748. int
  1749. i915_gem_mmap_gtt(struct drm_file *file,
  1750. struct drm_device *dev,
  1751. uint32_t handle,
  1752. uint64_t *offset)
  1753. {
  1754. struct drm_i915_gem_object *obj;
  1755. int ret;
  1756. obj = i915_gem_object_lookup(file, handle);
  1757. if (!obj)
  1758. return -ENOENT;
  1759. ret = i915_gem_object_create_mmap_offset(obj);
  1760. if (ret == 0)
  1761. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1762. i915_gem_object_put(obj);
  1763. return ret;
  1764. }
  1765. /**
  1766. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1767. * @dev: DRM device
  1768. * @data: GTT mapping ioctl data
  1769. * @file: GEM object info
  1770. *
  1771. * Simply returns the fake offset to userspace so it can mmap it.
  1772. * The mmap call will end up in drm_gem_mmap(), which will set things
  1773. * up so we can get faults in the handler above.
  1774. *
  1775. * The fault handler will take care of binding the object into the GTT
  1776. * (since it may have been evicted to make room for something), allocating
  1777. * a fence register, and mapping the appropriate aperture address into
  1778. * userspace.
  1779. */
  1780. int
  1781. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1782. struct drm_file *file)
  1783. {
  1784. struct drm_i915_gem_mmap_gtt *args = data;
  1785. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1786. }
  1787. /* Immediately discard the backing storage */
  1788. static void
  1789. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1790. {
  1791. i915_gem_object_free_mmap_offset(obj);
  1792. if (obj->base.filp == NULL)
  1793. return;
  1794. /* Our goal here is to return as much of the memory as
  1795. * is possible back to the system as we are called from OOM.
  1796. * To do this we must instruct the shmfs to drop all of its
  1797. * backing pages, *now*.
  1798. */
  1799. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1800. obj->mm.madv = __I915_MADV_PURGED;
  1801. }
  1802. /* Try to discard unwanted pages */
  1803. void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1804. {
  1805. struct address_space *mapping;
  1806. lockdep_assert_held(&obj->mm.lock);
  1807. GEM_BUG_ON(obj->mm.pages);
  1808. switch (obj->mm.madv) {
  1809. case I915_MADV_DONTNEED:
  1810. i915_gem_object_truncate(obj);
  1811. case __I915_MADV_PURGED:
  1812. return;
  1813. }
  1814. if (obj->base.filp == NULL)
  1815. return;
  1816. mapping = obj->base.filp->f_mapping,
  1817. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1818. }
  1819. static void
  1820. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
  1821. struct sg_table *pages)
  1822. {
  1823. struct sgt_iter sgt_iter;
  1824. struct page *page;
  1825. __i915_gem_object_release_shmem(obj, pages, true);
  1826. i915_gem_gtt_finish_pages(obj, pages);
  1827. if (i915_gem_object_needs_bit17_swizzle(obj))
  1828. i915_gem_object_save_bit_17_swizzle(obj, pages);
  1829. for_each_sgt_page(page, sgt_iter, pages) {
  1830. if (obj->mm.dirty)
  1831. set_page_dirty(page);
  1832. if (obj->mm.madv == I915_MADV_WILLNEED)
  1833. mark_page_accessed(page);
  1834. put_page(page);
  1835. }
  1836. obj->mm.dirty = false;
  1837. sg_free_table(pages);
  1838. kfree(pages);
  1839. }
  1840. static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
  1841. {
  1842. struct radix_tree_iter iter;
  1843. void **slot;
  1844. radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
  1845. radix_tree_delete(&obj->mm.get_page.radix, iter.index);
  1846. }
  1847. void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
  1848. enum i915_mm_subclass subclass)
  1849. {
  1850. struct sg_table *pages;
  1851. if (i915_gem_object_has_pinned_pages(obj))
  1852. return;
  1853. GEM_BUG_ON(obj->bind_count);
  1854. if (!READ_ONCE(obj->mm.pages))
  1855. return;
  1856. /* May be called by shrinker from within get_pages() (on another bo) */
  1857. mutex_lock_nested(&obj->mm.lock, subclass);
  1858. if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
  1859. goto unlock;
  1860. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1861. * array, hence protect them from being reaped by removing them from gtt
  1862. * lists early. */
  1863. pages = fetch_and_zero(&obj->mm.pages);
  1864. GEM_BUG_ON(!pages);
  1865. if (obj->mm.mapping) {
  1866. void *ptr;
  1867. ptr = ptr_mask_bits(obj->mm.mapping);
  1868. if (is_vmalloc_addr(ptr))
  1869. vunmap(ptr);
  1870. else
  1871. kunmap(kmap_to_page(ptr));
  1872. obj->mm.mapping = NULL;
  1873. }
  1874. __i915_gem_object_reset_page_iter(obj);
  1875. obj->ops->put_pages(obj, pages);
  1876. unlock:
  1877. mutex_unlock(&obj->mm.lock);
  1878. }
  1879. static void i915_sg_trim(struct sg_table *orig_st)
  1880. {
  1881. struct sg_table new_st;
  1882. struct scatterlist *sg, *new_sg;
  1883. unsigned int i;
  1884. if (orig_st->nents == orig_st->orig_nents)
  1885. return;
  1886. if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
  1887. return;
  1888. new_sg = new_st.sgl;
  1889. for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
  1890. sg_set_page(new_sg, sg_page(sg), sg->length, 0);
  1891. /* called before being DMA mapped, no need to copy sg->dma_* */
  1892. new_sg = sg_next(new_sg);
  1893. }
  1894. GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
  1895. sg_free_table(orig_st);
  1896. *orig_st = new_st;
  1897. }
  1898. static struct sg_table *
  1899. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1900. {
  1901. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1902. const unsigned long page_count = obj->base.size / PAGE_SIZE;
  1903. unsigned long i;
  1904. struct address_space *mapping;
  1905. struct sg_table *st;
  1906. struct scatterlist *sg;
  1907. struct sgt_iter sgt_iter;
  1908. struct page *page;
  1909. unsigned long last_pfn = 0; /* suppress gcc warning */
  1910. unsigned int max_segment;
  1911. int ret;
  1912. gfp_t gfp;
  1913. /* Assert that the object is not currently in any GPU domain. As it
  1914. * wasn't in the GTT, there shouldn't be any way it could have been in
  1915. * a GPU cache
  1916. */
  1917. GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1918. GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1919. max_segment = swiotlb_max_segment();
  1920. if (!max_segment)
  1921. max_segment = rounddown(UINT_MAX, PAGE_SIZE);
  1922. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1923. if (st == NULL)
  1924. return ERR_PTR(-ENOMEM);
  1925. rebuild_st:
  1926. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1927. kfree(st);
  1928. return ERR_PTR(-ENOMEM);
  1929. }
  1930. /* Get the list of pages out of our struct file. They'll be pinned
  1931. * at this point until we release them.
  1932. *
  1933. * Fail silently without starting the shrinker
  1934. */
  1935. mapping = obj->base.filp->f_mapping;
  1936. gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
  1937. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  1938. sg = st->sgl;
  1939. st->nents = 0;
  1940. for (i = 0; i < page_count; i++) {
  1941. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1942. if (IS_ERR(page)) {
  1943. i915_gem_shrink(dev_priv,
  1944. page_count,
  1945. I915_SHRINK_BOUND |
  1946. I915_SHRINK_UNBOUND |
  1947. I915_SHRINK_PURGEABLE);
  1948. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1949. }
  1950. if (IS_ERR(page)) {
  1951. /* We've tried hard to allocate the memory by reaping
  1952. * our own buffer, now let the real VM do its job and
  1953. * go down in flames if truly OOM.
  1954. */
  1955. page = shmem_read_mapping_page(mapping, i);
  1956. if (IS_ERR(page)) {
  1957. ret = PTR_ERR(page);
  1958. goto err_sg;
  1959. }
  1960. }
  1961. if (!i ||
  1962. sg->length >= max_segment ||
  1963. page_to_pfn(page) != last_pfn + 1) {
  1964. if (i)
  1965. sg = sg_next(sg);
  1966. st->nents++;
  1967. sg_set_page(sg, page, PAGE_SIZE, 0);
  1968. } else {
  1969. sg->length += PAGE_SIZE;
  1970. }
  1971. last_pfn = page_to_pfn(page);
  1972. /* Check that the i965g/gm workaround works. */
  1973. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  1974. }
  1975. if (sg) /* loop terminated early; short sg table */
  1976. sg_mark_end(sg);
  1977. /* Trim unused sg entries to avoid wasting memory. */
  1978. i915_sg_trim(st);
  1979. ret = i915_gem_gtt_prepare_pages(obj, st);
  1980. if (ret) {
  1981. /* DMA remapping failed? One possible cause is that
  1982. * it could not reserve enough large entries, asking
  1983. * for PAGE_SIZE chunks instead may be helpful.
  1984. */
  1985. if (max_segment > PAGE_SIZE) {
  1986. for_each_sgt_page(page, sgt_iter, st)
  1987. put_page(page);
  1988. sg_free_table(st);
  1989. max_segment = PAGE_SIZE;
  1990. goto rebuild_st;
  1991. } else {
  1992. dev_warn(&dev_priv->drm.pdev->dev,
  1993. "Failed to DMA remap %lu pages\n",
  1994. page_count);
  1995. goto err_pages;
  1996. }
  1997. }
  1998. if (i915_gem_object_needs_bit17_swizzle(obj))
  1999. i915_gem_object_do_bit_17_swizzle(obj, st);
  2000. return st;
  2001. err_sg:
  2002. sg_mark_end(sg);
  2003. err_pages:
  2004. for_each_sgt_page(page, sgt_iter, st)
  2005. put_page(page);
  2006. sg_free_table(st);
  2007. kfree(st);
  2008. /* shmemfs first checks if there is enough memory to allocate the page
  2009. * and reports ENOSPC should there be insufficient, along with the usual
  2010. * ENOMEM for a genuine allocation failure.
  2011. *
  2012. * We use ENOSPC in our driver to mean that we have run out of aperture
  2013. * space and so want to translate the error from shmemfs back to our
  2014. * usual understanding of ENOMEM.
  2015. */
  2016. if (ret == -ENOSPC)
  2017. ret = -ENOMEM;
  2018. return ERR_PTR(ret);
  2019. }
  2020. void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
  2021. struct sg_table *pages)
  2022. {
  2023. lockdep_assert_held(&obj->mm.lock);
  2024. obj->mm.get_page.sg_pos = pages->sgl;
  2025. obj->mm.get_page.sg_idx = 0;
  2026. obj->mm.pages = pages;
  2027. if (i915_gem_object_is_tiled(obj) &&
  2028. to_i915(obj->base.dev)->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  2029. GEM_BUG_ON(obj->mm.quirked);
  2030. __i915_gem_object_pin_pages(obj);
  2031. obj->mm.quirked = true;
  2032. }
  2033. }
  2034. static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  2035. {
  2036. struct sg_table *pages;
  2037. GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
  2038. if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
  2039. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  2040. return -EFAULT;
  2041. }
  2042. pages = obj->ops->get_pages(obj);
  2043. if (unlikely(IS_ERR(pages)))
  2044. return PTR_ERR(pages);
  2045. __i915_gem_object_set_pages(obj, pages);
  2046. return 0;
  2047. }
  2048. /* Ensure that the associated pages are gathered from the backing storage
  2049. * and pinned into our object. i915_gem_object_pin_pages() may be called
  2050. * multiple times before they are released by a single call to
  2051. * i915_gem_object_unpin_pages() - once the pages are no longer referenced
  2052. * either as a result of memory pressure (reaping pages under the shrinker)
  2053. * or as the object is itself released.
  2054. */
  2055. int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  2056. {
  2057. int err;
  2058. err = mutex_lock_interruptible(&obj->mm.lock);
  2059. if (err)
  2060. return err;
  2061. if (unlikely(!obj->mm.pages)) {
  2062. err = ____i915_gem_object_get_pages(obj);
  2063. if (err)
  2064. goto unlock;
  2065. smp_mb__before_atomic();
  2066. }
  2067. atomic_inc(&obj->mm.pages_pin_count);
  2068. unlock:
  2069. mutex_unlock(&obj->mm.lock);
  2070. return err;
  2071. }
  2072. /* The 'mapping' part of i915_gem_object_pin_map() below */
  2073. static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
  2074. enum i915_map_type type)
  2075. {
  2076. unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
  2077. struct sg_table *sgt = obj->mm.pages;
  2078. struct sgt_iter sgt_iter;
  2079. struct page *page;
  2080. struct page *stack_pages[32];
  2081. struct page **pages = stack_pages;
  2082. unsigned long i = 0;
  2083. pgprot_t pgprot;
  2084. void *addr;
  2085. /* A single page can always be kmapped */
  2086. if (n_pages == 1 && type == I915_MAP_WB)
  2087. return kmap(sg_page(sgt->sgl));
  2088. if (n_pages > ARRAY_SIZE(stack_pages)) {
  2089. /* Too big for stack -- allocate temporary array instead */
  2090. pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
  2091. if (!pages)
  2092. return NULL;
  2093. }
  2094. for_each_sgt_page(page, sgt_iter, sgt)
  2095. pages[i++] = page;
  2096. /* Check that we have the expected number of pages */
  2097. GEM_BUG_ON(i != n_pages);
  2098. switch (type) {
  2099. case I915_MAP_WB:
  2100. pgprot = PAGE_KERNEL;
  2101. break;
  2102. case I915_MAP_WC:
  2103. pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
  2104. break;
  2105. }
  2106. addr = vmap(pages, n_pages, 0, pgprot);
  2107. if (pages != stack_pages)
  2108. drm_free_large(pages);
  2109. return addr;
  2110. }
  2111. /* get, pin, and map the pages of the object into kernel space */
  2112. void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
  2113. enum i915_map_type type)
  2114. {
  2115. enum i915_map_type has_type;
  2116. bool pinned;
  2117. void *ptr;
  2118. int ret;
  2119. GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
  2120. ret = mutex_lock_interruptible(&obj->mm.lock);
  2121. if (ret)
  2122. return ERR_PTR(ret);
  2123. pinned = true;
  2124. if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
  2125. if (unlikely(!obj->mm.pages)) {
  2126. ret = ____i915_gem_object_get_pages(obj);
  2127. if (ret)
  2128. goto err_unlock;
  2129. smp_mb__before_atomic();
  2130. }
  2131. atomic_inc(&obj->mm.pages_pin_count);
  2132. pinned = false;
  2133. }
  2134. GEM_BUG_ON(!obj->mm.pages);
  2135. ptr = ptr_unpack_bits(obj->mm.mapping, has_type);
  2136. if (ptr && has_type != type) {
  2137. if (pinned) {
  2138. ret = -EBUSY;
  2139. goto err_unpin;
  2140. }
  2141. if (is_vmalloc_addr(ptr))
  2142. vunmap(ptr);
  2143. else
  2144. kunmap(kmap_to_page(ptr));
  2145. ptr = obj->mm.mapping = NULL;
  2146. }
  2147. if (!ptr) {
  2148. ptr = i915_gem_object_map(obj, type);
  2149. if (!ptr) {
  2150. ret = -ENOMEM;
  2151. goto err_unpin;
  2152. }
  2153. obj->mm.mapping = ptr_pack_bits(ptr, type);
  2154. }
  2155. out_unlock:
  2156. mutex_unlock(&obj->mm.lock);
  2157. return ptr;
  2158. err_unpin:
  2159. atomic_dec(&obj->mm.pages_pin_count);
  2160. err_unlock:
  2161. ptr = ERR_PTR(ret);
  2162. goto out_unlock;
  2163. }
  2164. static bool ban_context(const struct i915_gem_context *ctx)
  2165. {
  2166. return (i915_gem_context_is_bannable(ctx) &&
  2167. ctx->ban_score >= CONTEXT_SCORE_BAN_THRESHOLD);
  2168. }
  2169. static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
  2170. {
  2171. ctx->guilty_count++;
  2172. ctx->ban_score += CONTEXT_SCORE_GUILTY;
  2173. if (ban_context(ctx))
  2174. i915_gem_context_set_banned(ctx);
  2175. DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
  2176. ctx->name, ctx->ban_score,
  2177. yesno(i915_gem_context_is_banned(ctx)));
  2178. if (!i915_gem_context_is_banned(ctx) || IS_ERR_OR_NULL(ctx->file_priv))
  2179. return;
  2180. ctx->file_priv->context_bans++;
  2181. DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
  2182. ctx->name, ctx->file_priv->context_bans);
  2183. }
  2184. static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
  2185. {
  2186. ctx->active_count++;
  2187. }
  2188. struct drm_i915_gem_request *
  2189. i915_gem_find_active_request(struct intel_engine_cs *engine)
  2190. {
  2191. struct drm_i915_gem_request *request;
  2192. /* We are called by the error capture and reset at a random
  2193. * point in time. In particular, note that neither is crucially
  2194. * ordered with an interrupt. After a hang, the GPU is dead and we
  2195. * assume that no more writes can happen (we waited long enough for
  2196. * all writes that were in transaction to be flushed) - adding an
  2197. * extra delay for a recent interrupt is pointless. Hence, we do
  2198. * not need an engine->irq_seqno_barrier() before the seqno reads.
  2199. */
  2200. list_for_each_entry(request, &engine->timeline->requests, link) {
  2201. if (__i915_gem_request_completed(request))
  2202. continue;
  2203. GEM_BUG_ON(request->engine != engine);
  2204. return request;
  2205. }
  2206. return NULL;
  2207. }
  2208. static bool engine_stalled(struct intel_engine_cs *engine)
  2209. {
  2210. if (!engine->hangcheck.stalled)
  2211. return false;
  2212. /* Check for possible seqno movement after hang declaration */
  2213. if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
  2214. DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
  2215. return false;
  2216. }
  2217. return true;
  2218. }
  2219. int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
  2220. {
  2221. struct intel_engine_cs *engine;
  2222. enum intel_engine_id id;
  2223. int err = 0;
  2224. /* Ensure irq handler finishes, and not run again. */
  2225. for_each_engine(engine, dev_priv, id) {
  2226. struct drm_i915_gem_request *request;
  2227. tasklet_kill(&engine->irq_tasklet);
  2228. if (engine_stalled(engine)) {
  2229. request = i915_gem_find_active_request(engine);
  2230. if (request && request->fence.error == -EIO)
  2231. err = -EIO; /* Previous reset failed! */
  2232. }
  2233. }
  2234. i915_gem_revoke_fences(dev_priv);
  2235. return err;
  2236. }
  2237. static void skip_request(struct drm_i915_gem_request *request)
  2238. {
  2239. void *vaddr = request->ring->vaddr;
  2240. u32 head;
  2241. /* As this request likely depends on state from the lost
  2242. * context, clear out all the user operations leaving the
  2243. * breadcrumb at the end (so we get the fence notifications).
  2244. */
  2245. head = request->head;
  2246. if (request->postfix < head) {
  2247. memset(vaddr + head, 0, request->ring->size - head);
  2248. head = 0;
  2249. }
  2250. memset(vaddr + head, 0, request->postfix - head);
  2251. dma_fence_set_error(&request->fence, -EIO);
  2252. }
  2253. static void engine_skip_context(struct drm_i915_gem_request *request)
  2254. {
  2255. struct intel_engine_cs *engine = request->engine;
  2256. struct i915_gem_context *hung_ctx = request->ctx;
  2257. struct intel_timeline *timeline;
  2258. unsigned long flags;
  2259. timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
  2260. spin_lock_irqsave(&engine->timeline->lock, flags);
  2261. spin_lock(&timeline->lock);
  2262. list_for_each_entry_continue(request, &engine->timeline->requests, link)
  2263. if (request->ctx == hung_ctx)
  2264. skip_request(request);
  2265. list_for_each_entry(request, &timeline->requests, link)
  2266. skip_request(request);
  2267. spin_unlock(&timeline->lock);
  2268. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2269. }
  2270. /* Returns true if the request was guilty of hang */
  2271. static bool i915_gem_reset_request(struct drm_i915_gem_request *request)
  2272. {
  2273. /* Read once and return the resolution */
  2274. const bool guilty = engine_stalled(request->engine);
  2275. /* The guilty request will get skipped on a hung engine.
  2276. *
  2277. * Users of client default contexts do not rely on logical
  2278. * state preserved between batches so it is safe to execute
  2279. * queued requests following the hang. Non default contexts
  2280. * rely on preserved state, so skipping a batch loses the
  2281. * evolution of the state and it needs to be considered corrupted.
  2282. * Executing more queued batches on top of corrupted state is
  2283. * risky. But we take the risk by trying to advance through
  2284. * the queued requests in order to make the client behaviour
  2285. * more predictable around resets, by not throwing away random
  2286. * amount of batches it has prepared for execution. Sophisticated
  2287. * clients can use gem_reset_stats_ioctl and dma fence status
  2288. * (exported via sync_file info ioctl on explicit fences) to observe
  2289. * when it loses the context state and should rebuild accordingly.
  2290. *
  2291. * The context ban, and ultimately the client ban, mechanism are safety
  2292. * valves if client submission ends up resulting in nothing more than
  2293. * subsequent hangs.
  2294. */
  2295. if (guilty) {
  2296. i915_gem_context_mark_guilty(request->ctx);
  2297. skip_request(request);
  2298. } else {
  2299. i915_gem_context_mark_innocent(request->ctx);
  2300. dma_fence_set_error(&request->fence, -EAGAIN);
  2301. }
  2302. return guilty;
  2303. }
  2304. static void i915_gem_reset_engine(struct intel_engine_cs *engine)
  2305. {
  2306. struct drm_i915_gem_request *request;
  2307. if (engine->irq_seqno_barrier)
  2308. engine->irq_seqno_barrier(engine);
  2309. request = i915_gem_find_active_request(engine);
  2310. if (request && i915_gem_reset_request(request)) {
  2311. DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
  2312. engine->name, request->global_seqno);
  2313. /* If this context is now banned, skip all pending requests. */
  2314. if (i915_gem_context_is_banned(request->ctx))
  2315. engine_skip_context(request);
  2316. }
  2317. /* Setup the CS to resume from the breadcrumb of the hung request */
  2318. engine->reset_hw(engine, request);
  2319. }
  2320. void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
  2321. {
  2322. struct intel_engine_cs *engine;
  2323. enum intel_engine_id id;
  2324. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2325. i915_gem_retire_requests(dev_priv);
  2326. for_each_engine(engine, dev_priv, id)
  2327. i915_gem_reset_engine(engine);
  2328. i915_gem_restore_fences(dev_priv);
  2329. if (dev_priv->gt.awake) {
  2330. intel_sanitize_gt_powersave(dev_priv);
  2331. intel_enable_gt_powersave(dev_priv);
  2332. if (INTEL_GEN(dev_priv) >= 6)
  2333. gen6_rps_busy(dev_priv);
  2334. }
  2335. }
  2336. static void nop_submit_request(struct drm_i915_gem_request *request)
  2337. {
  2338. dma_fence_set_error(&request->fence, -EIO);
  2339. i915_gem_request_submit(request);
  2340. intel_engine_init_global_seqno(request->engine, request->global_seqno);
  2341. }
  2342. static void engine_set_wedged(struct intel_engine_cs *engine)
  2343. {
  2344. struct drm_i915_gem_request *request;
  2345. unsigned long flags;
  2346. /* We need to be sure that no thread is running the old callback as
  2347. * we install the nop handler (otherwise we would submit a request
  2348. * to hardware that will never complete). In order to prevent this
  2349. * race, we wait until the machine is idle before making the swap
  2350. * (using stop_machine()).
  2351. */
  2352. engine->submit_request = nop_submit_request;
  2353. /* Mark all executing requests as skipped */
  2354. spin_lock_irqsave(&engine->timeline->lock, flags);
  2355. list_for_each_entry(request, &engine->timeline->requests, link)
  2356. dma_fence_set_error(&request->fence, -EIO);
  2357. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2358. /* Mark all pending requests as complete so that any concurrent
  2359. * (lockless) lookup doesn't try and wait upon the request as we
  2360. * reset it.
  2361. */
  2362. intel_engine_init_global_seqno(engine,
  2363. intel_engine_last_submit(engine));
  2364. /*
  2365. * Clear the execlists queue up before freeing the requests, as those
  2366. * are the ones that keep the context and ringbuffer backing objects
  2367. * pinned in place.
  2368. */
  2369. if (i915.enable_execlists) {
  2370. unsigned long flags;
  2371. spin_lock_irqsave(&engine->timeline->lock, flags);
  2372. i915_gem_request_put(engine->execlist_port[0].request);
  2373. i915_gem_request_put(engine->execlist_port[1].request);
  2374. memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
  2375. engine->execlist_queue = RB_ROOT;
  2376. engine->execlist_first = NULL;
  2377. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2378. }
  2379. }
  2380. static int __i915_gem_set_wedged_BKL(void *data)
  2381. {
  2382. struct drm_i915_private *i915 = data;
  2383. struct intel_engine_cs *engine;
  2384. enum intel_engine_id id;
  2385. for_each_engine(engine, i915, id)
  2386. engine_set_wedged(engine);
  2387. return 0;
  2388. }
  2389. void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
  2390. {
  2391. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2392. set_bit(I915_WEDGED, &dev_priv->gpu_error.flags);
  2393. stop_machine(__i915_gem_set_wedged_BKL, dev_priv, NULL);
  2394. i915_gem_context_lost(dev_priv);
  2395. i915_gem_retire_requests(dev_priv);
  2396. mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
  2397. }
  2398. static void
  2399. i915_gem_retire_work_handler(struct work_struct *work)
  2400. {
  2401. struct drm_i915_private *dev_priv =
  2402. container_of(work, typeof(*dev_priv), gt.retire_work.work);
  2403. struct drm_device *dev = &dev_priv->drm;
  2404. /* Come back later if the device is busy... */
  2405. if (mutex_trylock(&dev->struct_mutex)) {
  2406. i915_gem_retire_requests(dev_priv);
  2407. mutex_unlock(&dev->struct_mutex);
  2408. }
  2409. /* Keep the retire handler running until we are finally idle.
  2410. * We do not need to do this test under locking as in the worst-case
  2411. * we queue the retire worker once too often.
  2412. */
  2413. if (READ_ONCE(dev_priv->gt.awake)) {
  2414. i915_queue_hangcheck(dev_priv);
  2415. queue_delayed_work(dev_priv->wq,
  2416. &dev_priv->gt.retire_work,
  2417. round_jiffies_up_relative(HZ));
  2418. }
  2419. }
  2420. static void
  2421. i915_gem_idle_work_handler(struct work_struct *work)
  2422. {
  2423. struct drm_i915_private *dev_priv =
  2424. container_of(work, typeof(*dev_priv), gt.idle_work.work);
  2425. struct drm_device *dev = &dev_priv->drm;
  2426. struct intel_engine_cs *engine;
  2427. enum intel_engine_id id;
  2428. bool rearm_hangcheck;
  2429. if (!READ_ONCE(dev_priv->gt.awake))
  2430. return;
  2431. /*
  2432. * Wait for last execlists context complete, but bail out in case a
  2433. * new request is submitted.
  2434. */
  2435. wait_for(READ_ONCE(dev_priv->gt.active_requests) ||
  2436. intel_execlists_idle(dev_priv), 10);
  2437. if (READ_ONCE(dev_priv->gt.active_requests))
  2438. return;
  2439. rearm_hangcheck =
  2440. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  2441. if (!mutex_trylock(&dev->struct_mutex)) {
  2442. /* Currently busy, come back later */
  2443. mod_delayed_work(dev_priv->wq,
  2444. &dev_priv->gt.idle_work,
  2445. msecs_to_jiffies(50));
  2446. goto out_rearm;
  2447. }
  2448. /*
  2449. * New request retired after this work handler started, extend active
  2450. * period until next instance of the work.
  2451. */
  2452. if (work_pending(work))
  2453. goto out_unlock;
  2454. if (dev_priv->gt.active_requests)
  2455. goto out_unlock;
  2456. if (wait_for(intel_execlists_idle(dev_priv), 10))
  2457. DRM_ERROR("Timeout waiting for engines to idle\n");
  2458. for_each_engine(engine, dev_priv, id)
  2459. i915_gem_batch_pool_fini(&engine->batch_pool);
  2460. GEM_BUG_ON(!dev_priv->gt.awake);
  2461. dev_priv->gt.awake = false;
  2462. rearm_hangcheck = false;
  2463. if (INTEL_GEN(dev_priv) >= 6)
  2464. gen6_rps_idle(dev_priv);
  2465. intel_runtime_pm_put(dev_priv);
  2466. out_unlock:
  2467. mutex_unlock(&dev->struct_mutex);
  2468. out_rearm:
  2469. if (rearm_hangcheck) {
  2470. GEM_BUG_ON(!dev_priv->gt.awake);
  2471. i915_queue_hangcheck(dev_priv);
  2472. }
  2473. }
  2474. void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
  2475. {
  2476. struct drm_i915_gem_object *obj = to_intel_bo(gem);
  2477. struct drm_i915_file_private *fpriv = file->driver_priv;
  2478. struct i915_vma *vma, *vn;
  2479. mutex_lock(&obj->base.dev->struct_mutex);
  2480. list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link)
  2481. if (vma->vm->file == fpriv)
  2482. i915_vma_close(vma);
  2483. if (i915_gem_object_is_active(obj) &&
  2484. !i915_gem_object_has_active_reference(obj)) {
  2485. i915_gem_object_set_active_reference(obj);
  2486. i915_gem_object_get(obj);
  2487. }
  2488. mutex_unlock(&obj->base.dev->struct_mutex);
  2489. }
  2490. static unsigned long to_wait_timeout(s64 timeout_ns)
  2491. {
  2492. if (timeout_ns < 0)
  2493. return MAX_SCHEDULE_TIMEOUT;
  2494. if (timeout_ns == 0)
  2495. return 0;
  2496. return nsecs_to_jiffies_timeout(timeout_ns);
  2497. }
  2498. /**
  2499. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2500. * @dev: drm device pointer
  2501. * @data: ioctl data blob
  2502. * @file: drm file pointer
  2503. *
  2504. * Returns 0 if successful, else an error is returned with the remaining time in
  2505. * the timeout parameter.
  2506. * -ETIME: object is still busy after timeout
  2507. * -ERESTARTSYS: signal interrupted the wait
  2508. * -ENONENT: object doesn't exist
  2509. * Also possible, but rare:
  2510. * -EAGAIN: GPU wedged
  2511. * -ENOMEM: damn
  2512. * -ENODEV: Internal IRQ fail
  2513. * -E?: The add request failed
  2514. *
  2515. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2516. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2517. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2518. * without holding struct_mutex the object may become re-busied before this
  2519. * function completes. A similar but shorter * race condition exists in the busy
  2520. * ioctl
  2521. */
  2522. int
  2523. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2524. {
  2525. struct drm_i915_gem_wait *args = data;
  2526. struct drm_i915_gem_object *obj;
  2527. ktime_t start;
  2528. long ret;
  2529. if (args->flags != 0)
  2530. return -EINVAL;
  2531. obj = i915_gem_object_lookup(file, args->bo_handle);
  2532. if (!obj)
  2533. return -ENOENT;
  2534. start = ktime_get();
  2535. ret = i915_gem_object_wait(obj,
  2536. I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
  2537. to_wait_timeout(args->timeout_ns),
  2538. to_rps_client(file));
  2539. if (args->timeout_ns > 0) {
  2540. args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
  2541. if (args->timeout_ns < 0)
  2542. args->timeout_ns = 0;
  2543. }
  2544. i915_gem_object_put(obj);
  2545. return ret;
  2546. }
  2547. static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
  2548. {
  2549. int ret, i;
  2550. for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
  2551. ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
  2552. if (ret)
  2553. return ret;
  2554. }
  2555. return 0;
  2556. }
  2557. int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
  2558. {
  2559. int ret;
  2560. if (flags & I915_WAIT_LOCKED) {
  2561. struct i915_gem_timeline *tl;
  2562. lockdep_assert_held(&i915->drm.struct_mutex);
  2563. list_for_each_entry(tl, &i915->gt.timelines, link) {
  2564. ret = wait_for_timeline(tl, flags);
  2565. if (ret)
  2566. return ret;
  2567. }
  2568. } else {
  2569. ret = wait_for_timeline(&i915->gt.global_timeline, flags);
  2570. if (ret)
  2571. return ret;
  2572. }
  2573. return 0;
  2574. }
  2575. void i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  2576. bool force)
  2577. {
  2578. /* If we don't have a page list set up, then we're not pinned
  2579. * to GPU, and we can ignore the cache flush because it'll happen
  2580. * again at bind time.
  2581. */
  2582. if (!obj->mm.pages)
  2583. return;
  2584. /*
  2585. * Stolen memory is always coherent with the GPU as it is explicitly
  2586. * marked as wc by the system, or the system is cache-coherent.
  2587. */
  2588. if (obj->stolen || obj->phys_handle)
  2589. return;
  2590. /* If the GPU is snooping the contents of the CPU cache,
  2591. * we do not need to manually clear the CPU cache lines. However,
  2592. * the caches are only snooped when the render cache is
  2593. * flushed/invalidated. As we always have to emit invalidations
  2594. * and flushes when moving into and out of the RENDER domain, correct
  2595. * snooping behaviour occurs naturally as the result of our domain
  2596. * tracking.
  2597. */
  2598. if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
  2599. obj->cache_dirty = true;
  2600. return;
  2601. }
  2602. trace_i915_gem_object_clflush(obj);
  2603. drm_clflush_sg(obj->mm.pages);
  2604. obj->cache_dirty = false;
  2605. }
  2606. /** Flushes the GTT write domain for the object if it's dirty. */
  2607. static void
  2608. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2609. {
  2610. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2611. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2612. return;
  2613. /* No actual flushing is required for the GTT write domain. Writes
  2614. * to it "immediately" go to main memory as far as we know, so there's
  2615. * no chipset flush. It also doesn't land in render cache.
  2616. *
  2617. * However, we do have to enforce the order so that all writes through
  2618. * the GTT land before any writes to the device, such as updates to
  2619. * the GATT itself.
  2620. *
  2621. * We also have to wait a bit for the writes to land from the GTT.
  2622. * An uncached read (i.e. mmio) seems to be ideal for the round-trip
  2623. * timing. This issue has only been observed when switching quickly
  2624. * between GTT writes and CPU reads from inside the kernel on recent hw,
  2625. * and it appears to only affect discrete GTT blocks (i.e. on LLC
  2626. * system agents we cannot reproduce this behaviour).
  2627. */
  2628. wmb();
  2629. if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv))
  2630. POSTING_READ(RING_ACTHD(dev_priv->engine[RCS]->mmio_base));
  2631. intel_fb_obj_flush(obj, false, write_origin(obj, I915_GEM_DOMAIN_GTT));
  2632. obj->base.write_domain = 0;
  2633. trace_i915_gem_object_change_domain(obj,
  2634. obj->base.read_domains,
  2635. I915_GEM_DOMAIN_GTT);
  2636. }
  2637. /** Flushes the CPU write domain for the object if it's dirty. */
  2638. static void
  2639. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2640. {
  2641. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2642. return;
  2643. i915_gem_clflush_object(obj, obj->pin_display);
  2644. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  2645. obj->base.write_domain = 0;
  2646. trace_i915_gem_object_change_domain(obj,
  2647. obj->base.read_domains,
  2648. I915_GEM_DOMAIN_CPU);
  2649. }
  2650. /**
  2651. * Moves a single object to the GTT read, and possibly write domain.
  2652. * @obj: object to act on
  2653. * @write: ask for write access or read only
  2654. *
  2655. * This function returns when the move is complete, including waiting on
  2656. * flushes to occur.
  2657. */
  2658. int
  2659. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2660. {
  2661. uint32_t old_write_domain, old_read_domains;
  2662. int ret;
  2663. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2664. ret = i915_gem_object_wait(obj,
  2665. I915_WAIT_INTERRUPTIBLE |
  2666. I915_WAIT_LOCKED |
  2667. (write ? I915_WAIT_ALL : 0),
  2668. MAX_SCHEDULE_TIMEOUT,
  2669. NULL);
  2670. if (ret)
  2671. return ret;
  2672. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2673. return 0;
  2674. /* Flush and acquire obj->pages so that we are coherent through
  2675. * direct access in memory with previous cached writes through
  2676. * shmemfs and that our cache domain tracking remains valid.
  2677. * For example, if the obj->filp was moved to swap without us
  2678. * being notified and releasing the pages, we would mistakenly
  2679. * continue to assume that the obj remained out of the CPU cached
  2680. * domain.
  2681. */
  2682. ret = i915_gem_object_pin_pages(obj);
  2683. if (ret)
  2684. return ret;
  2685. i915_gem_object_flush_cpu_write_domain(obj);
  2686. /* Serialise direct access to this object with the barriers for
  2687. * coherent writes from the GPU, by effectively invalidating the
  2688. * GTT domain upon first access.
  2689. */
  2690. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2691. mb();
  2692. old_write_domain = obj->base.write_domain;
  2693. old_read_domains = obj->base.read_domains;
  2694. /* It should now be out of any other write domains, and we can update
  2695. * the domain values for our changes.
  2696. */
  2697. GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2698. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2699. if (write) {
  2700. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2701. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2702. obj->mm.dirty = true;
  2703. }
  2704. trace_i915_gem_object_change_domain(obj,
  2705. old_read_domains,
  2706. old_write_domain);
  2707. i915_gem_object_unpin_pages(obj);
  2708. return 0;
  2709. }
  2710. /**
  2711. * Changes the cache-level of an object across all VMA.
  2712. * @obj: object to act on
  2713. * @cache_level: new cache level to set for the object
  2714. *
  2715. * After this function returns, the object will be in the new cache-level
  2716. * across all GTT and the contents of the backing storage will be coherent,
  2717. * with respect to the new cache-level. In order to keep the backing storage
  2718. * coherent for all users, we only allow a single cache level to be set
  2719. * globally on the object and prevent it from being changed whilst the
  2720. * hardware is reading from the object. That is if the object is currently
  2721. * on the scanout it will be set to uncached (or equivalent display
  2722. * cache coherency) and all non-MOCS GPU access will also be uncached so
  2723. * that all direct access to the scanout remains coherent.
  2724. */
  2725. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2726. enum i915_cache_level cache_level)
  2727. {
  2728. struct i915_vma *vma;
  2729. int ret;
  2730. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2731. if (obj->cache_level == cache_level)
  2732. return 0;
  2733. /* Inspect the list of currently bound VMA and unbind any that would
  2734. * be invalid given the new cache-level. This is principally to
  2735. * catch the issue of the CS prefetch crossing page boundaries and
  2736. * reading an invalid PTE on older architectures.
  2737. */
  2738. restart:
  2739. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2740. if (!drm_mm_node_allocated(&vma->node))
  2741. continue;
  2742. if (i915_vma_is_pinned(vma)) {
  2743. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2744. return -EBUSY;
  2745. }
  2746. if (i915_gem_valid_gtt_space(vma, cache_level))
  2747. continue;
  2748. ret = i915_vma_unbind(vma);
  2749. if (ret)
  2750. return ret;
  2751. /* As unbinding may affect other elements in the
  2752. * obj->vma_list (due to side-effects from retiring
  2753. * an active vma), play safe and restart the iterator.
  2754. */
  2755. goto restart;
  2756. }
  2757. /* We can reuse the existing drm_mm nodes but need to change the
  2758. * cache-level on the PTE. We could simply unbind them all and
  2759. * rebind with the correct cache-level on next use. However since
  2760. * we already have a valid slot, dma mapping, pages etc, we may as
  2761. * rewrite the PTE in the belief that doing so tramples upon less
  2762. * state and so involves less work.
  2763. */
  2764. if (obj->bind_count) {
  2765. /* Before we change the PTE, the GPU must not be accessing it.
  2766. * If we wait upon the object, we know that all the bound
  2767. * VMA are no longer active.
  2768. */
  2769. ret = i915_gem_object_wait(obj,
  2770. I915_WAIT_INTERRUPTIBLE |
  2771. I915_WAIT_LOCKED |
  2772. I915_WAIT_ALL,
  2773. MAX_SCHEDULE_TIMEOUT,
  2774. NULL);
  2775. if (ret)
  2776. return ret;
  2777. if (!HAS_LLC(to_i915(obj->base.dev)) &&
  2778. cache_level != I915_CACHE_NONE) {
  2779. /* Access to snoopable pages through the GTT is
  2780. * incoherent and on some machines causes a hard
  2781. * lockup. Relinquish the CPU mmaping to force
  2782. * userspace to refault in the pages and we can
  2783. * then double check if the GTT mapping is still
  2784. * valid for that pointer access.
  2785. */
  2786. i915_gem_release_mmap(obj);
  2787. /* As we no longer need a fence for GTT access,
  2788. * we can relinquish it now (and so prevent having
  2789. * to steal a fence from someone else on the next
  2790. * fence request). Note GPU activity would have
  2791. * dropped the fence as all snoopable access is
  2792. * supposed to be linear.
  2793. */
  2794. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2795. ret = i915_vma_put_fence(vma);
  2796. if (ret)
  2797. return ret;
  2798. }
  2799. } else {
  2800. /* We either have incoherent backing store and
  2801. * so no GTT access or the architecture is fully
  2802. * coherent. In such cases, existing GTT mmaps
  2803. * ignore the cache bit in the PTE and we can
  2804. * rewrite it without confusing the GPU or having
  2805. * to force userspace to fault back in its mmaps.
  2806. */
  2807. }
  2808. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2809. if (!drm_mm_node_allocated(&vma->node))
  2810. continue;
  2811. ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
  2812. if (ret)
  2813. return ret;
  2814. }
  2815. }
  2816. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU &&
  2817. cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  2818. obj->cache_dirty = true;
  2819. list_for_each_entry(vma, &obj->vma_list, obj_link)
  2820. vma->node.color = cache_level;
  2821. obj->cache_level = cache_level;
  2822. return 0;
  2823. }
  2824. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  2825. struct drm_file *file)
  2826. {
  2827. struct drm_i915_gem_caching *args = data;
  2828. struct drm_i915_gem_object *obj;
  2829. int err = 0;
  2830. rcu_read_lock();
  2831. obj = i915_gem_object_lookup_rcu(file, args->handle);
  2832. if (!obj) {
  2833. err = -ENOENT;
  2834. goto out;
  2835. }
  2836. switch (obj->cache_level) {
  2837. case I915_CACHE_LLC:
  2838. case I915_CACHE_L3_LLC:
  2839. args->caching = I915_CACHING_CACHED;
  2840. break;
  2841. case I915_CACHE_WT:
  2842. args->caching = I915_CACHING_DISPLAY;
  2843. break;
  2844. default:
  2845. args->caching = I915_CACHING_NONE;
  2846. break;
  2847. }
  2848. out:
  2849. rcu_read_unlock();
  2850. return err;
  2851. }
  2852. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  2853. struct drm_file *file)
  2854. {
  2855. struct drm_i915_private *i915 = to_i915(dev);
  2856. struct drm_i915_gem_caching *args = data;
  2857. struct drm_i915_gem_object *obj;
  2858. enum i915_cache_level level;
  2859. int ret = 0;
  2860. switch (args->caching) {
  2861. case I915_CACHING_NONE:
  2862. level = I915_CACHE_NONE;
  2863. break;
  2864. case I915_CACHING_CACHED:
  2865. /*
  2866. * Due to a HW issue on BXT A stepping, GPU stores via a
  2867. * snooped mapping may leave stale data in a corresponding CPU
  2868. * cacheline, whereas normally such cachelines would get
  2869. * invalidated.
  2870. */
  2871. if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
  2872. return -ENODEV;
  2873. level = I915_CACHE_LLC;
  2874. break;
  2875. case I915_CACHING_DISPLAY:
  2876. level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
  2877. break;
  2878. default:
  2879. return -EINVAL;
  2880. }
  2881. obj = i915_gem_object_lookup(file, args->handle);
  2882. if (!obj)
  2883. return -ENOENT;
  2884. if (obj->cache_level == level)
  2885. goto out;
  2886. ret = i915_gem_object_wait(obj,
  2887. I915_WAIT_INTERRUPTIBLE,
  2888. MAX_SCHEDULE_TIMEOUT,
  2889. to_rps_client(file));
  2890. if (ret)
  2891. goto out;
  2892. ret = i915_mutex_lock_interruptible(dev);
  2893. if (ret)
  2894. goto out;
  2895. ret = i915_gem_object_set_cache_level(obj, level);
  2896. mutex_unlock(&dev->struct_mutex);
  2897. out:
  2898. i915_gem_object_put(obj);
  2899. return ret;
  2900. }
  2901. /*
  2902. * Prepare buffer for display plane (scanout, cursors, etc).
  2903. * Can be called from an uninterruptible phase (modesetting) and allows
  2904. * any flushes to be pipelined (for pageflips).
  2905. */
  2906. struct i915_vma *
  2907. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  2908. u32 alignment,
  2909. const struct i915_ggtt_view *view)
  2910. {
  2911. struct i915_vma *vma;
  2912. u32 old_read_domains, old_write_domain;
  2913. int ret;
  2914. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2915. /* Mark the pin_display early so that we account for the
  2916. * display coherency whilst setting up the cache domains.
  2917. */
  2918. obj->pin_display++;
  2919. /* The display engine is not coherent with the LLC cache on gen6. As
  2920. * a result, we make sure that the pinning that is about to occur is
  2921. * done with uncached PTEs. This is lowest common denominator for all
  2922. * chipsets.
  2923. *
  2924. * However for gen6+, we could do better by using the GFDT bit instead
  2925. * of uncaching, which would allow us to flush all the LLC-cached data
  2926. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  2927. */
  2928. ret = i915_gem_object_set_cache_level(obj,
  2929. HAS_WT(to_i915(obj->base.dev)) ?
  2930. I915_CACHE_WT : I915_CACHE_NONE);
  2931. if (ret) {
  2932. vma = ERR_PTR(ret);
  2933. goto err_unpin_display;
  2934. }
  2935. /* As the user may map the buffer once pinned in the display plane
  2936. * (e.g. libkms for the bootup splash), we have to ensure that we
  2937. * always use map_and_fenceable for all scanout buffers. However,
  2938. * it may simply be too big to fit into mappable, in which case
  2939. * put it anyway and hope that userspace can cope (but always first
  2940. * try to preserve the existing ABI).
  2941. */
  2942. vma = ERR_PTR(-ENOSPC);
  2943. if (!view || view->type == I915_GGTT_VIEW_NORMAL)
  2944. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
  2945. PIN_MAPPABLE | PIN_NONBLOCK);
  2946. if (IS_ERR(vma)) {
  2947. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  2948. unsigned int flags;
  2949. /* Valleyview is definitely limited to scanning out the first
  2950. * 512MiB. Lets presume this behaviour was inherited from the
  2951. * g4x display engine and that all earlier gen are similarly
  2952. * limited. Testing suggests that it is a little more
  2953. * complicated than this. For example, Cherryview appears quite
  2954. * happy to scanout from anywhere within its global aperture.
  2955. */
  2956. flags = 0;
  2957. if (HAS_GMCH_DISPLAY(i915))
  2958. flags = PIN_MAPPABLE;
  2959. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
  2960. }
  2961. if (IS_ERR(vma))
  2962. goto err_unpin_display;
  2963. vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
  2964. /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
  2965. if (obj->cache_dirty || obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
  2966. i915_gem_clflush_object(obj, true);
  2967. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  2968. }
  2969. old_write_domain = obj->base.write_domain;
  2970. old_read_domains = obj->base.read_domains;
  2971. /* It should now be out of any other write domains, and we can update
  2972. * the domain values for our changes.
  2973. */
  2974. obj->base.write_domain = 0;
  2975. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2976. trace_i915_gem_object_change_domain(obj,
  2977. old_read_domains,
  2978. old_write_domain);
  2979. return vma;
  2980. err_unpin_display:
  2981. obj->pin_display--;
  2982. return vma;
  2983. }
  2984. void
  2985. i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
  2986. {
  2987. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  2988. if (WARN_ON(vma->obj->pin_display == 0))
  2989. return;
  2990. if (--vma->obj->pin_display == 0)
  2991. vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
  2992. /* Bump the LRU to try and avoid premature eviction whilst flipping */
  2993. i915_gem_object_bump_inactive_ggtt(vma->obj);
  2994. i915_vma_unpin(vma);
  2995. }
  2996. /**
  2997. * Moves a single object to the CPU read, and possibly write domain.
  2998. * @obj: object to act on
  2999. * @write: requesting write or read-only access
  3000. *
  3001. * This function returns when the move is complete, including waiting on
  3002. * flushes to occur.
  3003. */
  3004. int
  3005. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3006. {
  3007. uint32_t old_write_domain, old_read_domains;
  3008. int ret;
  3009. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3010. ret = i915_gem_object_wait(obj,
  3011. I915_WAIT_INTERRUPTIBLE |
  3012. I915_WAIT_LOCKED |
  3013. (write ? I915_WAIT_ALL : 0),
  3014. MAX_SCHEDULE_TIMEOUT,
  3015. NULL);
  3016. if (ret)
  3017. return ret;
  3018. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3019. return 0;
  3020. i915_gem_object_flush_gtt_write_domain(obj);
  3021. old_write_domain = obj->base.write_domain;
  3022. old_read_domains = obj->base.read_domains;
  3023. /* Flush the CPU cache if it's still invalid. */
  3024. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3025. i915_gem_clflush_object(obj, false);
  3026. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3027. }
  3028. /* It should now be out of any other write domains, and we can update
  3029. * the domain values for our changes.
  3030. */
  3031. GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3032. /* If we're writing through the CPU, then the GPU read domains will
  3033. * need to be invalidated at next use.
  3034. */
  3035. if (write) {
  3036. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3037. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3038. }
  3039. trace_i915_gem_object_change_domain(obj,
  3040. old_read_domains,
  3041. old_write_domain);
  3042. return 0;
  3043. }
  3044. /* Throttle our rendering by waiting until the ring has completed our requests
  3045. * emitted over 20 msec ago.
  3046. *
  3047. * Note that if we were to use the current jiffies each time around the loop,
  3048. * we wouldn't escape the function with any frames outstanding if the time to
  3049. * render a frame was over 20ms.
  3050. *
  3051. * This should get us reasonable parallelism between CPU and GPU but also
  3052. * relatively low latency when blocking on a particular request to finish.
  3053. */
  3054. static int
  3055. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3056. {
  3057. struct drm_i915_private *dev_priv = to_i915(dev);
  3058. struct drm_i915_file_private *file_priv = file->driver_priv;
  3059. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3060. struct drm_i915_gem_request *request, *target = NULL;
  3061. long ret;
  3062. /* ABI: return -EIO if already wedged */
  3063. if (i915_terminally_wedged(&dev_priv->gpu_error))
  3064. return -EIO;
  3065. spin_lock(&file_priv->mm.lock);
  3066. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3067. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3068. break;
  3069. /*
  3070. * Note that the request might not have been submitted yet.
  3071. * In which case emitted_jiffies will be zero.
  3072. */
  3073. if (!request->emitted_jiffies)
  3074. continue;
  3075. target = request;
  3076. }
  3077. if (target)
  3078. i915_gem_request_get(target);
  3079. spin_unlock(&file_priv->mm.lock);
  3080. if (target == NULL)
  3081. return 0;
  3082. ret = i915_wait_request(target,
  3083. I915_WAIT_INTERRUPTIBLE,
  3084. MAX_SCHEDULE_TIMEOUT);
  3085. i915_gem_request_put(target);
  3086. return ret < 0 ? ret : 0;
  3087. }
  3088. struct i915_vma *
  3089. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3090. const struct i915_ggtt_view *view,
  3091. u64 size,
  3092. u64 alignment,
  3093. u64 flags)
  3094. {
  3095. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  3096. struct i915_address_space *vm = &dev_priv->ggtt.base;
  3097. struct i915_vma *vma;
  3098. int ret;
  3099. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3100. vma = i915_vma_instance(obj, vm, view);
  3101. if (unlikely(IS_ERR(vma)))
  3102. return vma;
  3103. if (i915_vma_misplaced(vma, size, alignment, flags)) {
  3104. if (flags & PIN_NONBLOCK &&
  3105. (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)))
  3106. return ERR_PTR(-ENOSPC);
  3107. if (flags & PIN_MAPPABLE) {
  3108. /* If the required space is larger than the available
  3109. * aperture, we will not able to find a slot for the
  3110. * object and unbinding the object now will be in
  3111. * vain. Worse, doing so may cause us to ping-pong
  3112. * the object in and out of the Global GTT and
  3113. * waste a lot of cycles under the mutex.
  3114. */
  3115. if (vma->fence_size > dev_priv->ggtt.mappable_end)
  3116. return ERR_PTR(-E2BIG);
  3117. /* If NONBLOCK is set the caller is optimistically
  3118. * trying to cache the full object within the mappable
  3119. * aperture, and *must* have a fallback in place for
  3120. * situations where we cannot bind the object. We
  3121. * can be a little more lax here and use the fallback
  3122. * more often to avoid costly migrations of ourselves
  3123. * and other objects within the aperture.
  3124. *
  3125. * Half-the-aperture is used as a simple heuristic.
  3126. * More interesting would to do search for a free
  3127. * block prior to making the commitment to unbind.
  3128. * That caters for the self-harm case, and with a
  3129. * little more heuristics (e.g. NOFAULT, NOEVICT)
  3130. * we could try to minimise harm to others.
  3131. */
  3132. if (flags & PIN_NONBLOCK &&
  3133. vma->fence_size > dev_priv->ggtt.mappable_end / 2)
  3134. return ERR_PTR(-ENOSPC);
  3135. }
  3136. WARN(i915_vma_is_pinned(vma),
  3137. "bo is already pinned in ggtt with incorrect alignment:"
  3138. " offset=%08x, req.alignment=%llx,"
  3139. " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
  3140. i915_ggtt_offset(vma), alignment,
  3141. !!(flags & PIN_MAPPABLE),
  3142. i915_vma_is_map_and_fenceable(vma));
  3143. ret = i915_vma_unbind(vma);
  3144. if (ret)
  3145. return ERR_PTR(ret);
  3146. }
  3147. ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
  3148. if (ret)
  3149. return ERR_PTR(ret);
  3150. return vma;
  3151. }
  3152. static __always_inline unsigned int __busy_read_flag(unsigned int id)
  3153. {
  3154. /* Note that we could alias engines in the execbuf API, but
  3155. * that would be very unwise as it prevents userspace from
  3156. * fine control over engine selection. Ahem.
  3157. *
  3158. * This should be something like EXEC_MAX_ENGINE instead of
  3159. * I915_NUM_ENGINES.
  3160. */
  3161. BUILD_BUG_ON(I915_NUM_ENGINES > 16);
  3162. return 0x10000 << id;
  3163. }
  3164. static __always_inline unsigned int __busy_write_id(unsigned int id)
  3165. {
  3166. /* The uABI guarantees an active writer is also amongst the read
  3167. * engines. This would be true if we accessed the activity tracking
  3168. * under the lock, but as we perform the lookup of the object and
  3169. * its activity locklessly we can not guarantee that the last_write
  3170. * being active implies that we have set the same engine flag from
  3171. * last_read - hence we always set both read and write busy for
  3172. * last_write.
  3173. */
  3174. return id | __busy_read_flag(id);
  3175. }
  3176. static __always_inline unsigned int
  3177. __busy_set_if_active(const struct dma_fence *fence,
  3178. unsigned int (*flag)(unsigned int id))
  3179. {
  3180. struct drm_i915_gem_request *rq;
  3181. /* We have to check the current hw status of the fence as the uABI
  3182. * guarantees forward progress. We could rely on the idle worker
  3183. * to eventually flush us, but to minimise latency just ask the
  3184. * hardware.
  3185. *
  3186. * Note we only report on the status of native fences.
  3187. */
  3188. if (!dma_fence_is_i915(fence))
  3189. return 0;
  3190. /* opencode to_request() in order to avoid const warnings */
  3191. rq = container_of(fence, struct drm_i915_gem_request, fence);
  3192. if (i915_gem_request_completed(rq))
  3193. return 0;
  3194. return flag(rq->engine->exec_id);
  3195. }
  3196. static __always_inline unsigned int
  3197. busy_check_reader(const struct dma_fence *fence)
  3198. {
  3199. return __busy_set_if_active(fence, __busy_read_flag);
  3200. }
  3201. static __always_inline unsigned int
  3202. busy_check_writer(const struct dma_fence *fence)
  3203. {
  3204. if (!fence)
  3205. return 0;
  3206. return __busy_set_if_active(fence, __busy_write_id);
  3207. }
  3208. int
  3209. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3210. struct drm_file *file)
  3211. {
  3212. struct drm_i915_gem_busy *args = data;
  3213. struct drm_i915_gem_object *obj;
  3214. struct reservation_object_list *list;
  3215. unsigned int seq;
  3216. int err;
  3217. err = -ENOENT;
  3218. rcu_read_lock();
  3219. obj = i915_gem_object_lookup_rcu(file, args->handle);
  3220. if (!obj)
  3221. goto out;
  3222. /* A discrepancy here is that we do not report the status of
  3223. * non-i915 fences, i.e. even though we may report the object as idle,
  3224. * a call to set-domain may still stall waiting for foreign rendering.
  3225. * This also means that wait-ioctl may report an object as busy,
  3226. * where busy-ioctl considers it idle.
  3227. *
  3228. * We trade the ability to warn of foreign fences to report on which
  3229. * i915 engines are active for the object.
  3230. *
  3231. * Alternatively, we can trade that extra information on read/write
  3232. * activity with
  3233. * args->busy =
  3234. * !reservation_object_test_signaled_rcu(obj->resv, true);
  3235. * to report the overall busyness. This is what the wait-ioctl does.
  3236. *
  3237. */
  3238. retry:
  3239. seq = raw_read_seqcount(&obj->resv->seq);
  3240. /* Translate the exclusive fence to the READ *and* WRITE engine */
  3241. args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
  3242. /* Translate shared fences to READ set of engines */
  3243. list = rcu_dereference(obj->resv->fence);
  3244. if (list) {
  3245. unsigned int shared_count = list->shared_count, i;
  3246. for (i = 0; i < shared_count; ++i) {
  3247. struct dma_fence *fence =
  3248. rcu_dereference(list->shared[i]);
  3249. args->busy |= busy_check_reader(fence);
  3250. }
  3251. }
  3252. if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
  3253. goto retry;
  3254. err = 0;
  3255. out:
  3256. rcu_read_unlock();
  3257. return err;
  3258. }
  3259. int
  3260. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3261. struct drm_file *file_priv)
  3262. {
  3263. return i915_gem_ring_throttle(dev, file_priv);
  3264. }
  3265. int
  3266. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3267. struct drm_file *file_priv)
  3268. {
  3269. struct drm_i915_private *dev_priv = to_i915(dev);
  3270. struct drm_i915_gem_madvise *args = data;
  3271. struct drm_i915_gem_object *obj;
  3272. int err;
  3273. switch (args->madv) {
  3274. case I915_MADV_DONTNEED:
  3275. case I915_MADV_WILLNEED:
  3276. break;
  3277. default:
  3278. return -EINVAL;
  3279. }
  3280. obj = i915_gem_object_lookup(file_priv, args->handle);
  3281. if (!obj)
  3282. return -ENOENT;
  3283. err = mutex_lock_interruptible(&obj->mm.lock);
  3284. if (err)
  3285. goto out;
  3286. if (obj->mm.pages &&
  3287. i915_gem_object_is_tiled(obj) &&
  3288. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3289. if (obj->mm.madv == I915_MADV_WILLNEED) {
  3290. GEM_BUG_ON(!obj->mm.quirked);
  3291. __i915_gem_object_unpin_pages(obj);
  3292. obj->mm.quirked = false;
  3293. }
  3294. if (args->madv == I915_MADV_WILLNEED) {
  3295. GEM_BUG_ON(obj->mm.quirked);
  3296. __i915_gem_object_pin_pages(obj);
  3297. obj->mm.quirked = true;
  3298. }
  3299. }
  3300. if (obj->mm.madv != __I915_MADV_PURGED)
  3301. obj->mm.madv = args->madv;
  3302. /* if the object is no longer attached, discard its backing storage */
  3303. if (obj->mm.madv == I915_MADV_DONTNEED && !obj->mm.pages)
  3304. i915_gem_object_truncate(obj);
  3305. args->retained = obj->mm.madv != __I915_MADV_PURGED;
  3306. mutex_unlock(&obj->mm.lock);
  3307. out:
  3308. i915_gem_object_put(obj);
  3309. return err;
  3310. }
  3311. static void
  3312. frontbuffer_retire(struct i915_gem_active *active,
  3313. struct drm_i915_gem_request *request)
  3314. {
  3315. struct drm_i915_gem_object *obj =
  3316. container_of(active, typeof(*obj), frontbuffer_write);
  3317. intel_fb_obj_flush(obj, true, ORIGIN_CS);
  3318. }
  3319. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3320. const struct drm_i915_gem_object_ops *ops)
  3321. {
  3322. mutex_init(&obj->mm.lock);
  3323. INIT_LIST_HEAD(&obj->global_link);
  3324. INIT_LIST_HEAD(&obj->userfault_link);
  3325. INIT_LIST_HEAD(&obj->obj_exec_link);
  3326. INIT_LIST_HEAD(&obj->vma_list);
  3327. INIT_LIST_HEAD(&obj->batch_pool_link);
  3328. obj->ops = ops;
  3329. reservation_object_init(&obj->__builtin_resv);
  3330. obj->resv = &obj->__builtin_resv;
  3331. obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
  3332. init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
  3333. obj->mm.madv = I915_MADV_WILLNEED;
  3334. INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
  3335. mutex_init(&obj->mm.get_page.lock);
  3336. i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
  3337. }
  3338. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3339. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
  3340. I915_GEM_OBJECT_IS_SHRINKABLE,
  3341. .get_pages = i915_gem_object_get_pages_gtt,
  3342. .put_pages = i915_gem_object_put_pages_gtt,
  3343. };
  3344. struct drm_i915_gem_object *
  3345. i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
  3346. {
  3347. struct drm_i915_gem_object *obj;
  3348. struct address_space *mapping;
  3349. gfp_t mask;
  3350. int ret;
  3351. /* There is a prevalence of the assumption that we fit the object's
  3352. * page count inside a 32bit _signed_ variable. Let's document this and
  3353. * catch if we ever need to fix it. In the meantime, if you do spot
  3354. * such a local variable, please consider fixing!
  3355. */
  3356. if (WARN_ON(size >> PAGE_SHIFT > INT_MAX))
  3357. return ERR_PTR(-E2BIG);
  3358. if (overflows_type(size, obj->base.size))
  3359. return ERR_PTR(-E2BIG);
  3360. obj = i915_gem_object_alloc(dev_priv);
  3361. if (obj == NULL)
  3362. return ERR_PTR(-ENOMEM);
  3363. ret = drm_gem_object_init(&dev_priv->drm, &obj->base, size);
  3364. if (ret)
  3365. goto fail;
  3366. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3367. if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
  3368. /* 965gm cannot relocate objects above 4GiB. */
  3369. mask &= ~__GFP_HIGHMEM;
  3370. mask |= __GFP_DMA32;
  3371. }
  3372. mapping = obj->base.filp->f_mapping;
  3373. mapping_set_gfp_mask(mapping, mask);
  3374. i915_gem_object_init(obj, &i915_gem_object_ops);
  3375. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3376. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3377. if (HAS_LLC(dev_priv)) {
  3378. /* On some devices, we can have the GPU use the LLC (the CPU
  3379. * cache) for about a 10% performance improvement
  3380. * compared to uncached. Graphics requests other than
  3381. * display scanout are coherent with the CPU in
  3382. * accessing this cache. This means in this mode we
  3383. * don't need to clflush on the CPU side, and on the
  3384. * GPU side we only need to flush internal caches to
  3385. * get data visible to the CPU.
  3386. *
  3387. * However, we maintain the display planes as UC, and so
  3388. * need to rebind when first used as such.
  3389. */
  3390. obj->cache_level = I915_CACHE_LLC;
  3391. } else
  3392. obj->cache_level = I915_CACHE_NONE;
  3393. trace_i915_gem_object_create(obj);
  3394. return obj;
  3395. fail:
  3396. i915_gem_object_free(obj);
  3397. return ERR_PTR(ret);
  3398. }
  3399. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3400. {
  3401. /* If we are the last user of the backing storage (be it shmemfs
  3402. * pages or stolen etc), we know that the pages are going to be
  3403. * immediately released. In this case, we can then skip copying
  3404. * back the contents from the GPU.
  3405. */
  3406. if (obj->mm.madv != I915_MADV_WILLNEED)
  3407. return false;
  3408. if (obj->base.filp == NULL)
  3409. return true;
  3410. /* At first glance, this looks racy, but then again so would be
  3411. * userspace racing mmap against close. However, the first external
  3412. * reference to the filp can only be obtained through the
  3413. * i915_gem_mmap_ioctl() which safeguards us against the user
  3414. * acquiring such a reference whilst we are in the middle of
  3415. * freeing the object.
  3416. */
  3417. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3418. }
  3419. static void __i915_gem_free_objects(struct drm_i915_private *i915,
  3420. struct llist_node *freed)
  3421. {
  3422. struct drm_i915_gem_object *obj, *on;
  3423. mutex_lock(&i915->drm.struct_mutex);
  3424. intel_runtime_pm_get(i915);
  3425. llist_for_each_entry(obj, freed, freed) {
  3426. struct i915_vma *vma, *vn;
  3427. trace_i915_gem_object_destroy(obj);
  3428. GEM_BUG_ON(i915_gem_object_is_active(obj));
  3429. list_for_each_entry_safe(vma, vn,
  3430. &obj->vma_list, obj_link) {
  3431. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  3432. GEM_BUG_ON(i915_vma_is_active(vma));
  3433. vma->flags &= ~I915_VMA_PIN_MASK;
  3434. i915_vma_close(vma);
  3435. }
  3436. GEM_BUG_ON(!list_empty(&obj->vma_list));
  3437. GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
  3438. list_del(&obj->global_link);
  3439. }
  3440. intel_runtime_pm_put(i915);
  3441. mutex_unlock(&i915->drm.struct_mutex);
  3442. llist_for_each_entry_safe(obj, on, freed, freed) {
  3443. GEM_BUG_ON(obj->bind_count);
  3444. GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
  3445. if (obj->ops->release)
  3446. obj->ops->release(obj);
  3447. if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
  3448. atomic_set(&obj->mm.pages_pin_count, 0);
  3449. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  3450. GEM_BUG_ON(obj->mm.pages);
  3451. if (obj->base.import_attach)
  3452. drm_prime_gem_destroy(&obj->base, NULL);
  3453. reservation_object_fini(&obj->__builtin_resv);
  3454. drm_gem_object_release(&obj->base);
  3455. i915_gem_info_remove_obj(i915, obj->base.size);
  3456. kfree(obj->bit_17);
  3457. i915_gem_object_free(obj);
  3458. }
  3459. }
  3460. static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
  3461. {
  3462. struct llist_node *freed;
  3463. freed = llist_del_all(&i915->mm.free_list);
  3464. if (unlikely(freed))
  3465. __i915_gem_free_objects(i915, freed);
  3466. }
  3467. static void __i915_gem_free_work(struct work_struct *work)
  3468. {
  3469. struct drm_i915_private *i915 =
  3470. container_of(work, struct drm_i915_private, mm.free_work);
  3471. struct llist_node *freed;
  3472. /* All file-owned VMA should have been released by this point through
  3473. * i915_gem_close_object(), or earlier by i915_gem_context_close().
  3474. * However, the object may also be bound into the global GTT (e.g.
  3475. * older GPUs without per-process support, or for direct access through
  3476. * the GTT either for the user or for scanout). Those VMA still need to
  3477. * unbound now.
  3478. */
  3479. while ((freed = llist_del_all(&i915->mm.free_list)))
  3480. __i915_gem_free_objects(i915, freed);
  3481. }
  3482. static void __i915_gem_free_object_rcu(struct rcu_head *head)
  3483. {
  3484. struct drm_i915_gem_object *obj =
  3485. container_of(head, typeof(*obj), rcu);
  3486. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  3487. /* We can't simply use call_rcu() from i915_gem_free_object()
  3488. * as we need to block whilst unbinding, and the call_rcu
  3489. * task may be called from softirq context. So we take a
  3490. * detour through a worker.
  3491. */
  3492. if (llist_add(&obj->freed, &i915->mm.free_list))
  3493. schedule_work(&i915->mm.free_work);
  3494. }
  3495. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3496. {
  3497. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3498. if (obj->mm.quirked)
  3499. __i915_gem_object_unpin_pages(obj);
  3500. if (discard_backing_storage(obj))
  3501. obj->mm.madv = I915_MADV_DONTNEED;
  3502. /* Before we free the object, make sure any pure RCU-only
  3503. * read-side critical sections are complete, e.g.
  3504. * i915_gem_busy_ioctl(). For the corresponding synchronized
  3505. * lookup see i915_gem_object_lookup_rcu().
  3506. */
  3507. call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
  3508. }
  3509. void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
  3510. {
  3511. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3512. GEM_BUG_ON(i915_gem_object_has_active_reference(obj));
  3513. if (i915_gem_object_is_active(obj))
  3514. i915_gem_object_set_active_reference(obj);
  3515. else
  3516. i915_gem_object_put(obj);
  3517. }
  3518. static void assert_kernel_context_is_current(struct drm_i915_private *dev_priv)
  3519. {
  3520. struct intel_engine_cs *engine;
  3521. enum intel_engine_id id;
  3522. for_each_engine(engine, dev_priv, id)
  3523. GEM_BUG_ON(engine->last_retired_context &&
  3524. !i915_gem_context_is_kernel(engine->last_retired_context));
  3525. }
  3526. int i915_gem_suspend(struct drm_i915_private *dev_priv)
  3527. {
  3528. struct drm_device *dev = &dev_priv->drm;
  3529. int ret;
  3530. intel_suspend_gt_powersave(dev_priv);
  3531. mutex_lock(&dev->struct_mutex);
  3532. /* We have to flush all the executing contexts to main memory so
  3533. * that they can saved in the hibernation image. To ensure the last
  3534. * context image is coherent, we have to switch away from it. That
  3535. * leaves the dev_priv->kernel_context still active when
  3536. * we actually suspend, and its image in memory may not match the GPU
  3537. * state. Fortunately, the kernel_context is disposable and we do
  3538. * not rely on its state.
  3539. */
  3540. ret = i915_gem_switch_to_kernel_context(dev_priv);
  3541. if (ret)
  3542. goto err;
  3543. ret = i915_gem_wait_for_idle(dev_priv,
  3544. I915_WAIT_INTERRUPTIBLE |
  3545. I915_WAIT_LOCKED);
  3546. if (ret)
  3547. goto err;
  3548. i915_gem_retire_requests(dev_priv);
  3549. GEM_BUG_ON(dev_priv->gt.active_requests);
  3550. assert_kernel_context_is_current(dev_priv);
  3551. i915_gem_context_lost(dev_priv);
  3552. mutex_unlock(&dev->struct_mutex);
  3553. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  3554. cancel_delayed_work_sync(&dev_priv->gt.retire_work);
  3555. /* As the idle_work is rearming if it detects a race, play safe and
  3556. * repeat the flush until it is definitely idle.
  3557. */
  3558. while (flush_delayed_work(&dev_priv->gt.idle_work))
  3559. ;
  3560. i915_gem_drain_freed_objects(dev_priv);
  3561. /* Assert that we sucessfully flushed all the work and
  3562. * reset the GPU back to its idle, low power state.
  3563. */
  3564. WARN_ON(dev_priv->gt.awake);
  3565. WARN_ON(!intel_execlists_idle(dev_priv));
  3566. /*
  3567. * Neither the BIOS, ourselves or any other kernel
  3568. * expects the system to be in execlists mode on startup,
  3569. * so we need to reset the GPU back to legacy mode. And the only
  3570. * known way to disable logical contexts is through a GPU reset.
  3571. *
  3572. * So in order to leave the system in a known default configuration,
  3573. * always reset the GPU upon unload and suspend. Afterwards we then
  3574. * clean up the GEM state tracking, flushing off the requests and
  3575. * leaving the system in a known idle state.
  3576. *
  3577. * Note that is of the upmost importance that the GPU is idle and
  3578. * all stray writes are flushed *before* we dismantle the backing
  3579. * storage for the pinned objects.
  3580. *
  3581. * However, since we are uncertain that resetting the GPU on older
  3582. * machines is a good idea, we don't - just in case it leaves the
  3583. * machine in an unusable condition.
  3584. */
  3585. if (HAS_HW_CONTEXTS(dev_priv)) {
  3586. int reset = intel_gpu_reset(dev_priv, ALL_ENGINES);
  3587. WARN_ON(reset && reset != -ENODEV);
  3588. }
  3589. return 0;
  3590. err:
  3591. mutex_unlock(&dev->struct_mutex);
  3592. return ret;
  3593. }
  3594. void i915_gem_resume(struct drm_i915_private *dev_priv)
  3595. {
  3596. struct drm_device *dev = &dev_priv->drm;
  3597. WARN_ON(dev_priv->gt.awake);
  3598. mutex_lock(&dev->struct_mutex);
  3599. i915_gem_restore_gtt_mappings(dev_priv);
  3600. /* As we didn't flush the kernel context before suspend, we cannot
  3601. * guarantee that the context image is complete. So let's just reset
  3602. * it and start again.
  3603. */
  3604. dev_priv->gt.resume(dev_priv);
  3605. mutex_unlock(&dev->struct_mutex);
  3606. }
  3607. void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
  3608. {
  3609. if (INTEL_GEN(dev_priv) < 5 ||
  3610. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3611. return;
  3612. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3613. DISP_TILE_SURFACE_SWIZZLING);
  3614. if (IS_GEN5(dev_priv))
  3615. return;
  3616. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3617. if (IS_GEN6(dev_priv))
  3618. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3619. else if (IS_GEN7(dev_priv))
  3620. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3621. else if (IS_GEN8(dev_priv))
  3622. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  3623. else
  3624. BUG();
  3625. }
  3626. static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
  3627. {
  3628. I915_WRITE(RING_CTL(base), 0);
  3629. I915_WRITE(RING_HEAD(base), 0);
  3630. I915_WRITE(RING_TAIL(base), 0);
  3631. I915_WRITE(RING_START(base), 0);
  3632. }
  3633. static void init_unused_rings(struct drm_i915_private *dev_priv)
  3634. {
  3635. if (IS_I830(dev_priv)) {
  3636. init_unused_ring(dev_priv, PRB1_BASE);
  3637. init_unused_ring(dev_priv, SRB0_BASE);
  3638. init_unused_ring(dev_priv, SRB1_BASE);
  3639. init_unused_ring(dev_priv, SRB2_BASE);
  3640. init_unused_ring(dev_priv, SRB3_BASE);
  3641. } else if (IS_GEN2(dev_priv)) {
  3642. init_unused_ring(dev_priv, SRB0_BASE);
  3643. init_unused_ring(dev_priv, SRB1_BASE);
  3644. } else if (IS_GEN3(dev_priv)) {
  3645. init_unused_ring(dev_priv, PRB1_BASE);
  3646. init_unused_ring(dev_priv, PRB2_BASE);
  3647. }
  3648. }
  3649. int
  3650. i915_gem_init_hw(struct drm_i915_private *dev_priv)
  3651. {
  3652. struct intel_engine_cs *engine;
  3653. enum intel_engine_id id;
  3654. int ret;
  3655. dev_priv->gt.last_init_time = ktime_get();
  3656. /* Double layer security blanket, see i915_gem_init() */
  3657. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3658. if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
  3659. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3660. if (IS_HASWELL(dev_priv))
  3661. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
  3662. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  3663. if (HAS_PCH_NOP(dev_priv)) {
  3664. if (IS_IVYBRIDGE(dev_priv)) {
  3665. u32 temp = I915_READ(GEN7_MSG_CTL);
  3666. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3667. I915_WRITE(GEN7_MSG_CTL, temp);
  3668. } else if (INTEL_GEN(dev_priv) >= 7) {
  3669. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  3670. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  3671. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  3672. }
  3673. }
  3674. i915_gem_init_swizzling(dev_priv);
  3675. /*
  3676. * At least 830 can leave some of the unused rings
  3677. * "active" (ie. head != tail) after resume which
  3678. * will prevent c3 entry. Makes sure all unused rings
  3679. * are totally idle.
  3680. */
  3681. init_unused_rings(dev_priv);
  3682. BUG_ON(!dev_priv->kernel_context);
  3683. ret = i915_ppgtt_init_hw(dev_priv);
  3684. if (ret) {
  3685. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  3686. goto out;
  3687. }
  3688. /* Need to do basic initialisation of all rings first: */
  3689. for_each_engine(engine, dev_priv, id) {
  3690. ret = engine->init_hw(engine);
  3691. if (ret)
  3692. goto out;
  3693. }
  3694. intel_mocs_init_l3cc_table(dev_priv);
  3695. /* We can't enable contexts until all firmware is loaded */
  3696. ret = intel_guc_setup(dev_priv);
  3697. if (ret)
  3698. goto out;
  3699. out:
  3700. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3701. return ret;
  3702. }
  3703. bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
  3704. {
  3705. if (INTEL_INFO(dev_priv)->gen < 6)
  3706. return false;
  3707. /* TODO: make semaphores and Execlists play nicely together */
  3708. if (i915.enable_execlists)
  3709. return false;
  3710. if (value >= 0)
  3711. return value;
  3712. #ifdef CONFIG_INTEL_IOMMU
  3713. /* Enable semaphores on SNB when IO remapping is off */
  3714. if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
  3715. return false;
  3716. #endif
  3717. return true;
  3718. }
  3719. int i915_gem_init(struct drm_i915_private *dev_priv)
  3720. {
  3721. int ret;
  3722. mutex_lock(&dev_priv->drm.struct_mutex);
  3723. if (!i915.enable_execlists) {
  3724. dev_priv->gt.resume = intel_legacy_submission_resume;
  3725. dev_priv->gt.cleanup_engine = intel_engine_cleanup;
  3726. } else {
  3727. dev_priv->gt.resume = intel_lr_context_resume;
  3728. dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
  3729. }
  3730. /* This is just a security blanket to placate dragons.
  3731. * On some systems, we very sporadically observe that the first TLBs
  3732. * used by the CS may be stale, despite us poking the TLB reset. If
  3733. * we hold the forcewake during initialisation these problems
  3734. * just magically go away.
  3735. */
  3736. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3737. i915_gem_init_userptr(dev_priv);
  3738. ret = i915_gem_init_ggtt(dev_priv);
  3739. if (ret)
  3740. goto out_unlock;
  3741. ret = i915_gem_context_init(dev_priv);
  3742. if (ret)
  3743. goto out_unlock;
  3744. ret = intel_engines_init(dev_priv);
  3745. if (ret)
  3746. goto out_unlock;
  3747. ret = i915_gem_init_hw(dev_priv);
  3748. if (ret == -EIO) {
  3749. /* Allow engine initialisation to fail by marking the GPU as
  3750. * wedged. But we only want to do this where the GPU is angry,
  3751. * for all other failure, such as an allocation failure, bail.
  3752. */
  3753. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  3754. i915_gem_set_wedged(dev_priv);
  3755. ret = 0;
  3756. }
  3757. out_unlock:
  3758. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3759. mutex_unlock(&dev_priv->drm.struct_mutex);
  3760. return ret;
  3761. }
  3762. void
  3763. i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
  3764. {
  3765. struct intel_engine_cs *engine;
  3766. enum intel_engine_id id;
  3767. for_each_engine(engine, dev_priv, id)
  3768. dev_priv->gt.cleanup_engine(engine);
  3769. }
  3770. void
  3771. i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
  3772. {
  3773. int i;
  3774. if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
  3775. !IS_CHERRYVIEW(dev_priv))
  3776. dev_priv->num_fence_regs = 32;
  3777. else if (INTEL_INFO(dev_priv)->gen >= 4 ||
  3778. IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  3779. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
  3780. dev_priv->num_fence_regs = 16;
  3781. else
  3782. dev_priv->num_fence_regs = 8;
  3783. if (intel_vgpu_active(dev_priv))
  3784. dev_priv->num_fence_regs =
  3785. I915_READ(vgtif_reg(avail_rs.fence_num));
  3786. /* Initialize fence registers to zero */
  3787. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  3788. struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
  3789. fence->i915 = dev_priv;
  3790. fence->id = i;
  3791. list_add_tail(&fence->link, &dev_priv->mm.fence_list);
  3792. }
  3793. i915_gem_restore_fences(dev_priv);
  3794. i915_gem_detect_bit_6_swizzle(dev_priv);
  3795. }
  3796. int
  3797. i915_gem_load_init(struct drm_i915_private *dev_priv)
  3798. {
  3799. int err = -ENOMEM;
  3800. dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
  3801. if (!dev_priv->objects)
  3802. goto err_out;
  3803. dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
  3804. if (!dev_priv->vmas)
  3805. goto err_objects;
  3806. dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
  3807. SLAB_HWCACHE_ALIGN |
  3808. SLAB_RECLAIM_ACCOUNT |
  3809. SLAB_DESTROY_BY_RCU);
  3810. if (!dev_priv->requests)
  3811. goto err_vmas;
  3812. dev_priv->dependencies = KMEM_CACHE(i915_dependency,
  3813. SLAB_HWCACHE_ALIGN |
  3814. SLAB_RECLAIM_ACCOUNT);
  3815. if (!dev_priv->dependencies)
  3816. goto err_requests;
  3817. mutex_lock(&dev_priv->drm.struct_mutex);
  3818. INIT_LIST_HEAD(&dev_priv->gt.timelines);
  3819. err = i915_gem_timeline_init__global(dev_priv);
  3820. mutex_unlock(&dev_priv->drm.struct_mutex);
  3821. if (err)
  3822. goto err_dependencies;
  3823. INIT_LIST_HEAD(&dev_priv->context_list);
  3824. INIT_WORK(&dev_priv->mm.free_work, __i915_gem_free_work);
  3825. init_llist_head(&dev_priv->mm.free_list);
  3826. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  3827. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  3828. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3829. INIT_LIST_HEAD(&dev_priv->mm.userfault_list);
  3830. INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
  3831. i915_gem_retire_work_handler);
  3832. INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
  3833. i915_gem_idle_work_handler);
  3834. init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
  3835. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  3836. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  3837. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3838. dev_priv->mm.interruptible = true;
  3839. atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
  3840. spin_lock_init(&dev_priv->fb_tracking.lock);
  3841. return 0;
  3842. err_dependencies:
  3843. kmem_cache_destroy(dev_priv->dependencies);
  3844. err_requests:
  3845. kmem_cache_destroy(dev_priv->requests);
  3846. err_vmas:
  3847. kmem_cache_destroy(dev_priv->vmas);
  3848. err_objects:
  3849. kmem_cache_destroy(dev_priv->objects);
  3850. err_out:
  3851. return err;
  3852. }
  3853. void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
  3854. {
  3855. WARN_ON(!llist_empty(&dev_priv->mm.free_list));
  3856. mutex_lock(&dev_priv->drm.struct_mutex);
  3857. i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
  3858. WARN_ON(!list_empty(&dev_priv->gt.timelines));
  3859. mutex_unlock(&dev_priv->drm.struct_mutex);
  3860. kmem_cache_destroy(dev_priv->dependencies);
  3861. kmem_cache_destroy(dev_priv->requests);
  3862. kmem_cache_destroy(dev_priv->vmas);
  3863. kmem_cache_destroy(dev_priv->objects);
  3864. /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
  3865. rcu_barrier();
  3866. }
  3867. int i915_gem_freeze(struct drm_i915_private *dev_priv)
  3868. {
  3869. intel_runtime_pm_get(dev_priv);
  3870. mutex_lock(&dev_priv->drm.struct_mutex);
  3871. i915_gem_shrink_all(dev_priv);
  3872. mutex_unlock(&dev_priv->drm.struct_mutex);
  3873. intel_runtime_pm_put(dev_priv);
  3874. return 0;
  3875. }
  3876. int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
  3877. {
  3878. struct drm_i915_gem_object *obj;
  3879. struct list_head *phases[] = {
  3880. &dev_priv->mm.unbound_list,
  3881. &dev_priv->mm.bound_list,
  3882. NULL
  3883. }, **p;
  3884. /* Called just before we write the hibernation image.
  3885. *
  3886. * We need to update the domain tracking to reflect that the CPU
  3887. * will be accessing all the pages to create and restore from the
  3888. * hibernation, and so upon restoration those pages will be in the
  3889. * CPU domain.
  3890. *
  3891. * To make sure the hibernation image contains the latest state,
  3892. * we update that state just before writing out the image.
  3893. *
  3894. * To try and reduce the hibernation image, we manually shrink
  3895. * the objects as well.
  3896. */
  3897. mutex_lock(&dev_priv->drm.struct_mutex);
  3898. i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND);
  3899. for (p = phases; *p; p++) {
  3900. list_for_each_entry(obj, *p, global_link) {
  3901. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3902. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3903. }
  3904. }
  3905. mutex_unlock(&dev_priv->drm.struct_mutex);
  3906. return 0;
  3907. }
  3908. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3909. {
  3910. struct drm_i915_file_private *file_priv = file->driver_priv;
  3911. struct drm_i915_gem_request *request;
  3912. /* Clean up our request list when the client is going away, so that
  3913. * later retire_requests won't dereference our soon-to-be-gone
  3914. * file_priv.
  3915. */
  3916. spin_lock(&file_priv->mm.lock);
  3917. list_for_each_entry(request, &file_priv->mm.request_list, client_list)
  3918. request->file_priv = NULL;
  3919. spin_unlock(&file_priv->mm.lock);
  3920. if (!list_empty(&file_priv->rps.link)) {
  3921. spin_lock(&to_i915(dev)->rps.client_lock);
  3922. list_del(&file_priv->rps.link);
  3923. spin_unlock(&to_i915(dev)->rps.client_lock);
  3924. }
  3925. }
  3926. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  3927. {
  3928. struct drm_i915_file_private *file_priv;
  3929. int ret;
  3930. DRM_DEBUG("\n");
  3931. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  3932. if (!file_priv)
  3933. return -ENOMEM;
  3934. file->driver_priv = file_priv;
  3935. file_priv->dev_priv = to_i915(dev);
  3936. file_priv->file = file;
  3937. INIT_LIST_HEAD(&file_priv->rps.link);
  3938. spin_lock_init(&file_priv->mm.lock);
  3939. INIT_LIST_HEAD(&file_priv->mm.request_list);
  3940. file_priv->bsd_engine = -1;
  3941. ret = i915_gem_context_open(dev, file);
  3942. if (ret)
  3943. kfree(file_priv);
  3944. return ret;
  3945. }
  3946. /**
  3947. * i915_gem_track_fb - update frontbuffer tracking
  3948. * @old: current GEM buffer for the frontbuffer slots
  3949. * @new: new GEM buffer for the frontbuffer slots
  3950. * @frontbuffer_bits: bitmask of frontbuffer slots
  3951. *
  3952. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  3953. * from @old and setting them in @new. Both @old and @new can be NULL.
  3954. */
  3955. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  3956. struct drm_i915_gem_object *new,
  3957. unsigned frontbuffer_bits)
  3958. {
  3959. /* Control of individual bits within the mask are guarded by
  3960. * the owning plane->mutex, i.e. we can never see concurrent
  3961. * manipulation of individual bits. But since the bitfield as a whole
  3962. * is updated using RMW, we need to use atomics in order to update
  3963. * the bits.
  3964. */
  3965. BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
  3966. sizeof(atomic_t) * BITS_PER_BYTE);
  3967. if (old) {
  3968. WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
  3969. atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
  3970. }
  3971. if (new) {
  3972. WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
  3973. atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
  3974. }
  3975. }
  3976. /* Allocate a new GEM object and fill it with the supplied data */
  3977. struct drm_i915_gem_object *
  3978. i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
  3979. const void *data, size_t size)
  3980. {
  3981. struct drm_i915_gem_object *obj;
  3982. struct sg_table *sg;
  3983. size_t bytes;
  3984. int ret;
  3985. obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
  3986. if (IS_ERR(obj))
  3987. return obj;
  3988. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  3989. if (ret)
  3990. goto fail;
  3991. ret = i915_gem_object_pin_pages(obj);
  3992. if (ret)
  3993. goto fail;
  3994. sg = obj->mm.pages;
  3995. bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
  3996. obj->mm.dirty = true; /* Backing store is now out of date */
  3997. i915_gem_object_unpin_pages(obj);
  3998. if (WARN_ON(bytes != size)) {
  3999. DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
  4000. ret = -EFAULT;
  4001. goto fail;
  4002. }
  4003. return obj;
  4004. fail:
  4005. i915_gem_object_put(obj);
  4006. return ERR_PTR(ret);
  4007. }
  4008. struct scatterlist *
  4009. i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
  4010. unsigned int n,
  4011. unsigned int *offset)
  4012. {
  4013. struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
  4014. struct scatterlist *sg;
  4015. unsigned int idx, count;
  4016. might_sleep();
  4017. GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
  4018. GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
  4019. /* As we iterate forward through the sg, we record each entry in a
  4020. * radixtree for quick repeated (backwards) lookups. If we have seen
  4021. * this index previously, we will have an entry for it.
  4022. *
  4023. * Initial lookup is O(N), but this is amortized to O(1) for
  4024. * sequential page access (where each new request is consecutive
  4025. * to the previous one). Repeated lookups are O(lg(obj->base.size)),
  4026. * i.e. O(1) with a large constant!
  4027. */
  4028. if (n < READ_ONCE(iter->sg_idx))
  4029. goto lookup;
  4030. mutex_lock(&iter->lock);
  4031. /* We prefer to reuse the last sg so that repeated lookup of this
  4032. * (or the subsequent) sg are fast - comparing against the last
  4033. * sg is faster than going through the radixtree.
  4034. */
  4035. sg = iter->sg_pos;
  4036. idx = iter->sg_idx;
  4037. count = __sg_page_count(sg);
  4038. while (idx + count <= n) {
  4039. unsigned long exception, i;
  4040. int ret;
  4041. /* If we cannot allocate and insert this entry, or the
  4042. * individual pages from this range, cancel updating the
  4043. * sg_idx so that on this lookup we are forced to linearly
  4044. * scan onwards, but on future lookups we will try the
  4045. * insertion again (in which case we need to be careful of
  4046. * the error return reporting that we have already inserted
  4047. * this index).
  4048. */
  4049. ret = radix_tree_insert(&iter->radix, idx, sg);
  4050. if (ret && ret != -EEXIST)
  4051. goto scan;
  4052. exception =
  4053. RADIX_TREE_EXCEPTIONAL_ENTRY |
  4054. idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
  4055. for (i = 1; i < count; i++) {
  4056. ret = radix_tree_insert(&iter->radix, idx + i,
  4057. (void *)exception);
  4058. if (ret && ret != -EEXIST)
  4059. goto scan;
  4060. }
  4061. idx += count;
  4062. sg = ____sg_next(sg);
  4063. count = __sg_page_count(sg);
  4064. }
  4065. scan:
  4066. iter->sg_pos = sg;
  4067. iter->sg_idx = idx;
  4068. mutex_unlock(&iter->lock);
  4069. if (unlikely(n < idx)) /* insertion completed by another thread */
  4070. goto lookup;
  4071. /* In case we failed to insert the entry into the radixtree, we need
  4072. * to look beyond the current sg.
  4073. */
  4074. while (idx + count <= n) {
  4075. idx += count;
  4076. sg = ____sg_next(sg);
  4077. count = __sg_page_count(sg);
  4078. }
  4079. *offset = n - idx;
  4080. return sg;
  4081. lookup:
  4082. rcu_read_lock();
  4083. sg = radix_tree_lookup(&iter->radix, n);
  4084. GEM_BUG_ON(!sg);
  4085. /* If this index is in the middle of multi-page sg entry,
  4086. * the radixtree will contain an exceptional entry that points
  4087. * to the start of that range. We will return the pointer to
  4088. * the base page and the offset of this page within the
  4089. * sg entry's range.
  4090. */
  4091. *offset = 0;
  4092. if (unlikely(radix_tree_exception(sg))) {
  4093. unsigned long base =
  4094. (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
  4095. sg = radix_tree_lookup(&iter->radix, base);
  4096. GEM_BUG_ON(!sg);
  4097. *offset = n - base;
  4098. }
  4099. rcu_read_unlock();
  4100. return sg;
  4101. }
  4102. struct page *
  4103. i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
  4104. {
  4105. struct scatterlist *sg;
  4106. unsigned int offset;
  4107. GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
  4108. sg = i915_gem_object_get_sg(obj, n, &offset);
  4109. return nth_page(sg_page(sg), offset);
  4110. }
  4111. /* Like i915_gem_object_get_page(), but mark the returned page dirty */
  4112. struct page *
  4113. i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
  4114. unsigned int n)
  4115. {
  4116. struct page *page;
  4117. page = i915_gem_object_get_page(obj, n);
  4118. if (!obj->mm.dirty)
  4119. set_page_dirty(page);
  4120. return page;
  4121. }
  4122. dma_addr_t
  4123. i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
  4124. unsigned long n)
  4125. {
  4126. struct scatterlist *sg;
  4127. unsigned int offset;
  4128. sg = i915_gem_object_get_sg(obj, n, &offset);
  4129. return sg_dma_address(sg) + (offset << PAGE_SHIFT);
  4130. }